.NETGURU
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




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification