2

When should I use:

Int32 myint = 0;//Framework data type

vs

int myint = 0;//C# language data type

What are the similarities/differences? I seem to recall being told that there are performance differences? Is this true?

1
  • 2
    Do you mean Int32 instead of Int? Commented Jul 30, 2009 at 17:38

4 Answers 4

8

In C# the language-defined keywords for data types are just aliases for their respective BCL types. So int is exactly the same as Int32, long is the same as Int64, etc.

In general I'd think it's best to stick with the language-defined type names, though. There are definitely no performance differences as those aliases are resolved by the compiler.

Sign up to request clarification or add additional context in comments.

5 Comments

My C# compiler happily recognized Main with System.String[]. Where did you get this bit from?
Remember someone having this problem here on SO. Might have been wrong, didn't try. I removed that bit.
Visual studio has a problem recognizing Main(String[]) as a valid Main.
Note, this is not true in some other languages (like Java) where using Integer generates a Boxed reference type.
nader: The question was about .NET. And the dichotomy of primitives and wrapper classes is usually clear to most people writing Java :)
5

int is an alias for System.Int32, string is an alias for System.String. There are no differences.

That said, it's better practice to use int and string, not their System equivalent, mostly for readability. If you use a source style checker such as StyleCop, it will enforce replacing all System.String and System.Int32 declarations with their shorter equivalent.

2 Comments

Since usually all source files have a using System; at the top you wouldn't have to qualify the type name with its namespace on each use :)
You wouldn't have to due to the using statement, but in the case of int and string, you would never have to.
1

I used to advocate using C# language aliases over CLR types, but I changed my preference to using CLR types after I heard that Jeffery Richter advocates the CLR type in his book CLR via C#, my first hand dealing with other devs new to the CLR, the ability to see that the CLR, instead of C#, is the center of attention, as well as color clues between reference and value types helps in visually parsing code quickly.

Comments

1

Interesting: The compiler doesn’t allow you to type enum using the “Int32” but it does when you use “int”.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.