C# eq of C++ typedef

D

Dennis Myrén

Hi.

Is there any way to define an alias for a type like you could in C++:
typedef int NUMBER;

I tried using #define but that did not work.

Thank you.
 
J

Jon Skeet [C# MVP]

Dennis Myrén said:
Is there any way to define an alias for a type like you could in C++:
typedef int NUMBER;

I tried using #define but that did not work.

There's no way of doing that in a global way. You can use:

using NUMBER = System.Int32;

but that only applies to that source file.
 
D

Dennis Myrén

Hmm that is a shame.
I would really like that feature.

Well, thank you mr. Skeet.

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
Dennis Myrén said:
Is there any way to define an alias for a type like you could in C++:
typedef int NUMBER;

I tried using #define but that did not work.

There's no way of doing that in a global way. You can use:

using NUMBER = System.Int32;

but that only applies to that source file.
 
I

Ian Griffiths

This is exactly equivalent to a C++ typedef though.

Jon mentioned the non-global nature of such a using directive, but what he
didn't mention is that it's no different in C++. A C++ typedef also only has
any effect in the compilation units in which it appears.

The underlying difference between C# and C++ that applies here is that C#
doesn't support #include. With C++, you could put such a typedef in a header
file, and then #include that header file from multiple other source files.
This enables you to make the typedef appear in multiple compilation units.

So possibly the thing you think you really want is the ability to do
#includes. But the ability to define aliases for types is exactly as
powerful in both languages.
 
D

Dennis Myrén

Yes you are absolutelt right.

I could use the
using NUMBER = SYstem.Int32;
approach which mr. Skeet suggested.

My problem here is that i want it to be global.
For example, i have this interface where i want to create an alias for
System.Int32.
So, i would place that using directive on top of the module containing that
interface.

Then, I want all implementors(a lot of them) to be able to use that alias as
well, without redefining
it in their respective modules. Otherwise, it just does not make sense, if
you know what i mean.

But as I understand, there is no way to do that? Not even some workaround?

Finally(can't be) is there any overhead with the using directive in this
context?
 
S

Sami Vaaraniemi

Ian Griffiths said:
This is exactly equivalent to a C++ typedef though.

Jon mentioned the non-global nature of such a using directive, but what he
didn't mention is that it's no different in C++. A C++ typedef also only has
any effect in the compilation units in which it appears.


Just a minor nitpick, but they aren't *exactly* the same. In C++ you can use
typedefs in class or struct scope like so:

template <class Iterator>
struct iterator_traits {
typedef typename Iterator::iterator_category iterator_category;
typedef typename Iterator::value_type value_type;
typedef typename Iterator::difference_type difference_type;
typedef typename Iterator::pointer pointer;
typedef typename Iterator::reference reference;
};

These types can then be referred to from outside of the scope. C#'s type
aliasing mechanism does not allow aliasing within a class (or struct) scope.

I do miss typedef's since moving from C++ to C#. With generics in C# 2.0,
the need for typedefs is only going to grow.

Regards,
Sami
 
S

Scott Allen

I thought I was going to miss typedefs and preprocessor macros - but
I've found, generally speaking, c# to be more 'readable' without these
features.
 
E

Etienne Boucher

I agree that dropping #define and typedef was an improvement.

If you really need a custom type, you can use a struct with a private field.

Etienne Boucher
 

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

Similar Threads

Typedef in C#? 6
simple typedef equivalent in C# 5
Another c header conversion. Please help ! 2
typedef in c#? 4
typedef Replacement? 7
typedef 1
Typedef in C#? 14
PtrToStructure for pointer allocated in C++ 1

Top