C# eq of C++ typedef
Messages   Related Types
This message was discovered on microsoft.public.dotnet.languages.csharp.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.
Post a new message to this list...

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.
--
Regards,
Dennis JD Myrén
Oslo Kodebureau

Reply to this message...
 
    
Jon Skeet [C# MVP] (VIP)
Dennis Myrén <Click here to reveal e-mail address> wrote:
[Original message clipped]

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.

--
Jon Skeet - <Click here to reveal e-mail address>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Reply to this message...
 
    
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
"Jon Skeet [C# MVP]" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
Dennis Myrén <Click here to reveal e-mail address> wrote:
[Original message clipped]

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.

--
Jon Skeet - <Click here to reveal e-mail address>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Reply to this message...
 
    
Ian Griffiths (VIP)
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.

--
Ian Griffiths - http://www.interact-sw.co.uk/iangblog/
DevelopMentor - http://www.develop.com/

"Dennis Myrén wrote:
[Original message clipped]

Reply to this message...
 
    
Sami Vaaraniemi
"Ian Griffiths" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

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

[Original message clipped]

Reply to this message...
 
    
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.

--
Scott
http://www.OdeToCode.com

On Wed, 15 Sep 2004 14:25:07 +0300, "Sami Vaaraniemi"
<Click here to reveal e-mail address> wrote:

[Original message clipped]

Reply to this message...
 
    
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?

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Ian Griffiths" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
 
System.Int32




Ad
MBR BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
 
 Copyright © Matthew Baxter-Reynolds 2001-2008. '.NET 247 Software Development Services' is a trading style of MBR IT Solutions Ltd.
Contact Us - Terms of Use - Privacy Policy - www.dotnet247.com