4

While coding we can use C# native data types as well as .NET CTS types. I am curious to know which data type should I use while declaring any variable. I found somewhere that we should use c# native datatype while I believe we should use CTS Type as in IL every data type is going to be in converted in respective CTS type. But I am not sure still which I should use? Let me know your views.

Thanks.

marshalprince
  • 105
  • 4
  • 11

3 Answers3

4

C# native types are compiled to EXACTLY the same IL code as their System.* counterparts.

So

int x = 1;

is exactly the same as:

Int32 x = 1;

See this question for the complete picture:

C#, int or Int32? Should I care?

Community
  • 1
  • 1
Philippe Leybaert
  • 168,566
  • 31
  • 210
  • 223
  • so should we use .NET CTS types as it will use less time to convert? But then why c# native should be used is suggested? – marshalprince Jan 19 '10 at 09:56
  • I would never use the CTS types in my code. I don't see any reason for doing that. The language provides native datatypes for a reason. And your code looks cleaner if you use native type names. – Philippe Leybaert Jan 19 '10 at 10:00
  • but let if I am working in c# and using c++ file, in c++ if I use int that will be int16 but in c# it will be int32, won't I get some trouble in there? – marshalprince Jan 19 '10 at 10:07
  • 1
    int Visual C++, "int" is also Int32 – Philippe Leybaert Jan 19 '10 at 10:10
  • OK, I found one exception then and that is long. VC++ this is same as Int32 while in c# it is Int64. But the intention is not that, there may be some other conflict too or not? – marshalprince Jan 19 '10 at 10:16
1

The makers of the .NET framework recommend using the native C# types in the .NET Framework Design Guidelines book.

AaronLS
  • 37,329
  • 20
  • 143
  • 202
0

Jon Skeet outlines a few pros and cons in this answer.

Community
  • 1
  • 1
Heinzi
  • 167,459
  • 57
  • 363
  • 519