473,224 Members | 1,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,224 software developers and data experts.

Type alias in C#

Maybe I'm stupid or maybe I am missing the obvious, but I can't find a way
to define a type alias in C# for primitive types (class and interfaces I can
inherit from, no problem). I.e I want to declare a type that is an alias
for, say, int. Conceptually, this is what I want to do:

public MyIntType = int; // won't compile

Anyone knows how to do this?

--
Regards, Peter
Nov 7 '06
59 12098
Good grief!

To recap, I was the original user of sloppy semantics in this chain:

MG ...it won't work with interface, static, sealed, struct...
CI...It also won't work with value types...
MG...Hence "struct" in that list...

OK people, that was a slightly lazy bit of terminology! I used "struct"
(a C# keyword) interchangeably with "ValueType" (a CLR concept). Oops!
It seems to have been a catalyst for some faily lively debate, though,
so maybe it wasn't such a bad thing! If only I had said "structure"...
although that too seems up for debate here.

But

Nov 8 '06 #51
Understood - I believe that I inappropriately used the term "struct"
when referring to "structure" in some cases.
Of course, VB *does* use the term "Structure" to refer to the same kind of
type as a C# "struct".

Best Regards,
Dustin Campbell
Developer Express Inc.
Nov 8 '06 #52
Interesting, thanks. However, that means either that definition is
wrong or the docs that state System.Enum and all the FCL primitives
are structures is wrong :)
In the case of System.Enum, I think that System.Enum *is* a structure. However,
IMO, the jury is still out on whether types that derive from System.Enum are.

Best Regards,
Dustin Campbell
Developer Express Inc.
Nov 8 '06 #53
Because of that definition there is some ambiguity in the term now,
however I'm still not sure that one small paragraph should invalidate
the use of the term throughout the FCL documentation. Do you?
I think that referring to this as "one small paragraph" is quite an understatement.
I mean, if you can't trust the definition in the *glossary*, what can you
trust?

Best Regards,
Dustin Campbell
Developer Express Inc.
Nov 8 '06 #54
Dave Sexton wrote:
| Hi Chris,
|
| Understood - I believe that I inappropriately used the term "struct"
| when referring to "structure" in some cases.
|
| And BTW, I believe that the definition of "structure" that you cited
| should really be the definition of the C# keyword "struct". Don't
| you agree?

Yes, I do. Instead of saying "not all value types are structures" I should
have said "not all value types are C# struct(s)"

--
Christopher Ireland

"Getting married for sex is like buying a 747 for the free peanuts."
Jeff Foxworthy
Nov 8 '06 #55
Hi Dustin,
I think that referring to this as "one small paragraph" is quite an
understatement. I mean, if you can't trust the definition in the *glossary*,
what can you trust?
Good point :)

I trust the fact that every document in the MSDN library for all FCL
ValueTypes (of course, I haven't verified ALL) explicitly state each as
"Structure" in their titles. The MSDN documentation for System.ValueType uses
the term "structure" as well.

Interestingly enough, I searched the C# glossary, "struct" keyword definition
and some programming guidance documentation (all C#) but none contained the
term "structure" in any related way. (It's used to describe a "data
structure" though).

In VB.NET, as you mentioned in another post in this thread, the term
"Structure" appears to mean value-type. It's used for constraining generic
arguments. You have the choice of "Class", "Structure" or neither keyword in
VB.NET. In C#, "class", "struct" or neither keyword may be used to constrain
generic arguments after the "where" statement.

If you use the C# keyword "struct" to constrain a generic argument you can
successfully pass in an enum value:

public enum AnEnum { A, B }

class Program
{
static void Test<T>(T value)
where T : struct
{
Console.WriteLine("Testing: " + value.ToString());
}

static void Main(string[] args)
{
Test<int>(5);
Test<AnEnum>(AnEnum.B);

Console.WriteLine("Done");
Console.ReadLine();
}
}

Output:

Testing: 5
Testing: B
Done

So, it seems that even the C# compiler will treat types derived from Enum as
structs. The C# "enum" keyword was really the only thing preventing me from
thinking that "structure" was the same as "struct". But now it's obvious to
me that the "enum" keyword simply replaces "struct", providing a more specific
way (and only way) of declaring enums in the C# language.

I believe now that "enum" (C#) is a more-derived concept, but still describes
a "struct". Since all "structs" are "value-types", including "enums", and
since the term "structure" refers to all ValueTypes as well [MSDN docs], I
must conclude that the term "structure" is equal to "struct" and that "enum"
can be referred to as "enum", "struct", or "structure", in the same way that
"XmlDocument" may be referred to as "object". The term "structure", I think
of as a language-agnostic way to describe a value-type. The term "struct",
when used outside of a C# context can be used interchangeably with
"structure", since they mean the same thing. In a context that deals with the
C# compiler directly, I'll try to refrain from using "structure" as it
obviously has no place within the C# language itself. When speaking of the
VB.NET compiler it may be used freely because it apparently means the same
thing as the C# "struct" keyword.

The glossary needs to be updated ;)

I don't think that in the context of this thread the term "struct" was misused
by anyone, including Mark, who claims to have been the first to misuse the
term. Anytime "struct" or "structure" was used, IIRC, value-type was "meant",
and that seems now to have been just fine.

--
Dave Sexton
Nov 8 '06 #56
Hi Dustin,
>
>I think that referring to this as "one small paragraph" is quite an
understatement. I mean, if you can't trust the definition in the
*glossary*, what can you trust?
Good point :)

I trust the fact that every document in the MSDN library for all FCL
ValueTypes (of course, I haven't verified ALL) explicitly state each
as "Structure" in their titles.
Except that all FCL enumerations (which are also ValueTypes) list "Enumeration"
in their titles. Since enumerations are ValueTypes, that statement isn't
really correct. So, why does your logic for the presence of the keyword "Structure"
not apply to the presence of the keyword "Enumeration" as well? It seems
like you are trying to use this title keyword to prove your point but it
really seems to prove mine -- that enumerations are not "structures".
Interestingly enough, I searched the C# glossary, "struct" keyword
definition and some programming guidance documentation (all C#) but
none contained the term "structure" in any related way. (It's used to
describe a "data structure" though).

In VB.NET, as you mentioned in another post in this thread, the term
"Structure" appears to mean value-type. It's used for constraining
generic arguments. You have the choice of "Class", "Structure" or
neither keyword in VB.NET. In C#, "class", "struct" or neither
keyword may be used to constrain generic arguments after the "where"
statement.

If you use the C# keyword "struct" to constrain a generic argument you
can successfully pass in an enum value:
That indeed is a good point but that constraint is really only ensuring that
a type argument will descend from System.ValueType -- not that it is a C#
struct. So, its use on enumerations could simply be considered a side effect.

I just can't understand how an enum can be considered a structure when it
only shares one feature of structures: stack allocation. Other features such
as interface implementation and method declaration are not supported on enums.
And, using your own argument, the MSDN documentation never refers to an enumeration
as a "structure". Therefore, I have to conclude that enums *are* different
from structs. They are different kind of entity that are also allocated on
the stack. Of course, I'd really *like* to see Microsoft make enumerations
more like standard structures. It seems to me that it would be extremely
useful to have instances methods and interface support for enum types.

Best Regards,
Dustin Campbell
Developer Express Inc.
Nov 9 '06 #57
Hi Dustin,
>>I think that referring to this as "one small paragraph" is quite an
understatement. I mean, if you can't trust the definition in the
*glossary*, what can you trust?
Good point :)

I trust the fact that every document in the MSDN library for all FCL
ValueTypes (of course, I haven't verified ALL) explicitly state each
as "Structure" in their titles.

Except that all FCL enumerations (which are also ValueTypes) list
"Enumeration" in their titles. Since enumerations are ValueTypes, that
statement isn't really correct. So, why does your logic for the presence of
the keyword "Structure" not apply to the presence of the keyword
"Enumeration" as well? It seems like you are trying to use this title
keyword to prove your point but it really seems to prove mine -- that
enumerations are not "structures".
I see enums as restricted structures, but structures nonetheless because they
inherit from System.Enum, which is a structure.

The fact that the docs for FCL enumerations don't have "Structure" in their
titles is because, IMO, it would be redundant to write "DayOfWeek Enumeration
Structure", for example. I don't believe the lack of the term "structure" in
the titles proves anything. My proof is based on the presence of the term,
not its absence.
>Interestingly enough, I searched the C# glossary, "struct" keyword
definition and some programming guidance documentation (all C#) but
none contained the term "structure" in any related way. (It's used to
describe a "data structure" though).

In VB.NET, as you mentioned in another post in this thread, the term
"Structure" appears to mean value-type. It's used for constraining
generic arguments. You have the choice of "Class", "Structure" or
neither keyword in VB.NET. In C#, "class", "struct" or neither
keyword may be used to constrain generic arguments after the "where"
statement.

If you use the C# keyword "struct" to constrain a generic argument you
can successfully pass in an enum value:

That indeed is a good point but that constraint is really only ensuring that
a type argument will descend from System.ValueType -- not that it is a C#
struct. So, its use on enumerations could simply be considered a side
effect.
I don't think so. They could have easily used the keyword "enum", or the
keyword "value" to represent all value-types. I believe they didn't because
it's natural to think of enums as structs, although in terms of the C#
compiler and defining structures the literal meaning of "struct" doesn't
represent the literal meaning of "enum". These terms are obviously
overloaded, so depending on the context in which they are used they may or may
not be interchangeable.
I just can't understand how an enum can be considered a structure when it
only shares one feature of structures: stack allocation. Other features such
as interface implementation and method declaration are not supported on
enums.
Well it shares more than one feature through inheritance, including stack
allocation.

Enum-derived types inherit the GetHashCode and Equals implementations, as
wells as operator overloads (I assume) that are all part of the "structure" on
which Enums are based (provided by System.ValueType).

I don't believe that the limitations imposed on enums, after inheritance,
takes anything away from their derived nature. They behave like structs, and
you can use them like structs as parameters. You can even cast them down to
ValueType and use them as such. As a matter of fact, I've created library
methods that take System.Enum as a parameter. So, at what point does the
"structure" that I'm accepting as a parameter become "not" a structure? Even
a ValueType, for instance, is still technically an "object".
And, using your own argument, the MSDN documentation never refers to an
enumeration as a "structure". Therefore, I have to conclude that enums *are*
different from structs.
Again, my argument doesn't depend on the absence of any keywords, but their
presence. Presence has a lot more weight :)

The DayOfWeek Enumeration documentation, for example, doesn't mention anything
about it being a "value-type" or an "object" either.
They are different kind of entity that are also allocated on the stack. Of
course, I'd really *like* to see Microsoft make enumerations more like
standard structures. It seems to me that it would be extremely useful to
have instances methods and interface support for enum types.
Jon Skeet had proposed something he calls "class enums" that you might be
interested in reading (if you haven't already :)

"Enhanced Enums in C#"
http://msmvps.com/blogs/jon.skeet/ar...classenum.aspx

--
Dave Sexton
Nov 9 '06 #58
Jon Skeet had proposed something he calls "class enums" that you might
be interested in reading (if you haven't already :)

"Enhanced Enums in C#"
http://msmvps.com/blogs/jon.skeet/ar...classenum.aspx
Hi Dave,

You might also consider my own article here: http://diditwith.net/PermaLink,guid,...05cef6aad.aspx.
:-)

Thanks for the lively debate.

Best Regards,
Dustin Campbell
Developer Express Inc.
Nov 10 '06 #59
Hi Dustin,

Thanks for the discussion, it has been informative.

I'll take a look at your article when I get a chance to read it thoroughly.

--
Dave Sexton

"Dustin Campbell" <du*****@no-spam-pleasedevexpress.comwrote in message
news:c1**************************@news.microsoft.c om...
>Jon Skeet had proposed something he calls "class enums" that you might
be interested in reading (if you haven't already :)

"Enhanced Enums in C#"
http://msmvps.com/blogs/jon.skeet/ar...classenum.aspx

Hi Dave,

You might also consider my own article here:
http://diditwith.net/PermaLink,guid,...05cef6aad.aspx.
:-)

Thanks for the lively debate.

Best Regards,
Dustin Campbell
Developer Express Inc.


Nov 10 '06 #60

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: David Ford | last post by:
I have a macro that I use across the board for freeing ram. I'd like to clean up my code so I don't get these warnings. #define sfree(x) _internal_sfree((void **)&x) #define _internal_sfree(x)...
26
by: Adam Warner | last post by:
Hello all, I'm very new to C but I have a number of years of Common Lisp programming experience. I'm trying to figure out ways of translating higher order concepts such as closures into C. The...
2
by: Scott Ribe | last post by:
Here's my issue: Data comes in and is inserted into the database automatically, into a base table A. Later a human looks at the records, fills in some data, and now the records are assigned to...
6
by: Maxim | last post by:
Hello, everybody! I'd like to make global type alias. If I used c++ I would write the following code to create a global type alias: typedef SomeType MyAlias; I've found that using keyword...
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
7
by: chaz | last post by:
Hi, * this class is defined in assembly XXXXClsLib : Public class VVDB : inherits DBFuncs ... * Assembly YYYYFwApi has a reference to XXXXClsLib * this class B in YYYYFwApi has an imports stmt...
5
by: Alias | last post by:
Hi - I'm trying to implement a custom RoleProvider based on the SqlRoleProvider. I keep receiving a an error that it can't load type 'MyRoleTest.MyRoleProvider' when trying to load my...
6
by: dillogimp | last post by:
hi This code is taken from a lisp interpreter by Andru Luvisi. typedef struct obj { enum otype type; struct obj *p; // array of pointer, sizeof(obj)=8 } obj; typedef obj * (*primop)(obj *);...
1
by: Slapo | last post by:
Hello everyone, I could use some help with this function I've created (see below) that runs flawlessly on PostgreSQL 8.3.6 on my development machine but ends with an error on the production server...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.