473,659 Members | 2,663 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2454
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.co m>
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.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
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.co m>
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******** ******@TK2MSFTN GP09.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******** ******@TK2MSFTN GP09.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::itera tor_category iterator_catego ry;
typedef typename Iterator::value _type value_type;
typedef typename Iterator::diffe rence_type difference_type ;
typedef typename Iterator::point er pointer;
typedef typename Iterator::refer ence 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**********@p leasejippii.fi> wrote:

"Ian Griffiths" <ia************ *@nospam.nospam > wrote in message
news:uW******* *******@TK2MSFT NGP09.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::itera tor_category iterator_catego ry;
typedef typename Iterator::value _type value_type;
typedef typename Iterator::diffe rence_type difference_type ;
typedef typename Iterator::point er pointer;
typedef typename Iterator::refer ence 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
3627
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 support. (first i compiled them with g++3.x. ERR means compiler will bark, otherwise it does accept it. Then the Comeau C/C++ 4.3.3 comes)
14
4628
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 explanations, so I wonder is there any good using typedef ? and I also know that when a data type being typedefed become an abstract data type, so what exactly is an abstract data type, is it any good ? -- Posted via http://dbforums.com
4
3046
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 { DOUBLE_LIST, INT_LIST } DATA_TYPE; typedef struct { DATA_TYPE type;
15
5650
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 everything in a /*...*/ comment and appending the 'hail world' code. Is there an easier, and, let's say, more
16
3825
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
15633
by: vvv | last post by:
Hi All, Do we have anything in .NET which is equivalent to C++'s Typedef . Regards, Vasanth
6
7311
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 (condition) typedef char T; #else typedef short T;
15
2559
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 ! ) what is it supposed to do ?
12
4637
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 (*(*(*ptr2d_fptr)()))();
16
2766
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 threads, there is sound advice ( to me at any rate) not to hide pointers behind typedefs. So, may I ask the group when the use of typedefs really makes sense? Sorry if this is somewhat general, but there are no exercises ( not that I am asking for...
0
8427
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8330
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8746
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8626
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6178
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4334
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1975
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1737
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.