473,405 Members | 2,349 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,405 software developers and data experts.

C# eq of C++ typedef

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
Nov 16 '05 #1
7 2436
Dennis Myrén <de****@oslokb.no> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
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]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Dennis Myrén <de****@oslokb.no> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3
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:
Hmm that is a shame.
I would really like that feature.

Well, thank you mr. Skeet.

Jon Skeet wrote:
Dennis Myrén <de****@oslokb.no> wrote:
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.

Nov 16 '05 #4
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" <ia*************@nospam.nospam> wrote in message
news:uW**************@TK2MSFTNGP09.phx.gbl...
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:
Hmm that is a shame.
I would really like that feature.

Well, thank you mr. Skeet.

Jon Skeet wrote:
Dennis Myrén <de****@oslokb.no> wrote:
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.


Nov 16 '05 #5

"Ian Griffiths" <ia*************@nospam.nospam> wrote in message
news:uW**************@TK2MSFTNGP09.phx.gbl...
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

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:
Hmm that is a shame.
I would really like that feature.

Well, thank you mr. Skeet.

Jon Skeet wrote:
Dennis Myrén <de****@oslokb.no> wrote:
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.


Nov 16 '05 #6
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"
<sa**********@pleasejippii.fi> wrote:

"Ian Griffiths" <ia*************@nospam.nospam> wrote in message
news:uW**************@TK2MSFTNGP09.phx.gbl...
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

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:
> Hmm that is a shame.
> I would really like that feature.
>
> Well, thank you mr. Skeet.
>
> Jon Skeet wrote:
> Dennis Myrén <de****@oslokb.no> wrote:
>> 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.



Nov 16 '05 #7
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
Nov 16 '05 #8

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

Similar topics

2
by: joe | last post by:
hi, after reading some articles and faq, i want to clarify myself what's correct(conform to standard) and what's not? or what should be correct but it isn't simply because compilers don't...
14
by: dreamcatcher | last post by:
I always have this idea that typedef a data type especially a structure is very convenient in coding, but my teacher insisted that I should use the full struct declaration and no further...
4
by: Chris | last post by:
I've lurked around long enough... Time to interract =) I'm trying to make sense of the following. I can't quite wrap my head around what this is actually doing: ------------- typedef enum {...
15
by: Merrill & Michele | last post by:
typedef struct { WORD versionNumber; WORD offset; } MENUITEMTEMPLATEHEADER; This is from vol 5 of unnamed platform's programmer's reference. I could make this conforming by enclosing...
16
by: burn | last post by:
Hello, i am writing a program under linux in c and compile my code with make and gcc. Now i have 4 files: init.c/h and packets.c/h. Each header-file contains some: init.h: struct xyz {
12
by: vvv | last post by:
Hi All, Do we have anything in .NET which is equivalent to C++'s Typedef . Regards, Vasanth
6
by: Alex | last post by:
Hello people, I am getting errors from VS2003 when working with typedef'ed types. For example, assume that I have a type T, defined in a 3rd party include file based on some condition #if...
15
by: Ian Bush | last post by:
Hi All, I'm a bit confused by the following which is causing one of our user's codes fail in compilation: typedef struct SctpDest_S; 1) Is this standard ? 2) If so ( or even if not so ! )...
12
by: Googy | last post by:
Hi!! Can any one explain me the meaning of following notations clearly : 1. typedef char(*(*frpapfrc()))(); frpapfrc f; 2. typedef int (*(arr2d_ptr)()); arr2d_ptr p; 3. typedef int...
16
by: mdh | last post by:
A quick ? :-) question about Typedefs. There is a very brief discussion about this in K&R ( p146). Googling this group, there is a surprising dearth of questions about these. From one of the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.