Typedef in C#?

J

j.ignatius

I'm new to C# and come from a C++ background. C++ allows you to define
new types very easily like this:

typedef char array25_t[25];

which defines a type called array25_t that contains up to 25 chars.

Does a similar typedef also exist in C#? Or do you have to define a
new class and assign member variables?
 
J

James Curran

typedef char array25_t[25];

which defines a type called array25_t that contains up to 25 chars.

Does a similar typedef also exist in C#? Or do you have to define a
new class and assign member variables?

Not in C# v1.0.

In C# v2.0 (due out next month), you can do something similar with the
"using" directive.
Your example would translate to
using array25_t = char[25];

Except that still won't work, because the real problem is the "char[25]"
which is not allowed in C#.
 
J

Jon Skeet [C# MVP]

James Curran said:
typedef char array25_t[25];

which defines a type called array25_t that contains up to 25 chars.

Does a similar typedef also exist in C#? Or do you have to define a
new class and assign member variables?

Not in C# v1.0.

In C# v2.0 (due out next month), you can do something similar with the
"using" directive.
Your example would translate to
using array25_t = char[25];

Except that still won't work, because the real problem is the "char[25]"
which is not allowed in C#.

Has the using directive changed in 2.0 other than in terms of generics?
You can already do something like:

using Foo = System.String;

if you want in 1.1, although it's relatively rarely useful IME.

In 2.0 it's useful due to being able to do things like:

using NameAgeMap = System.Collections.Generic.Dictionary<string,int>;

Unfortunately, it doesn't stop you from doing:
using NameHeightMap =System.Collections.Generic.Dictionary<string,int>;

and then:

NameAgeMap x = new NameHeightMap();

Never mind :)
 
R

riscy

Jon, wondering what wrong with that :- NameAgeMap x = new
NameHeightMap() ?
Regards
Riscy
 
J

Jon Skeet [C# MVP]

Jon, wondering what wrong with that :- NameAgeMap x = new
NameHeightMap() ?

Well, there's nothing wrong as far as the compiler is concerned - but
clearly a height isn't the same as an age, so it doesn't make semantic
sense. It's as wrong as doing:

person.Height = person.Age;

Yes, they might both be integers, but as humans we know that they're
different types - a length of time isn't the same as a height.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top