1

I've been using "Int32", "String", and "Boolean" instead of "int", "string", "bool" in C# for a while now, but I don't know why. Why does this matter? Which is "better"? What's the difference?

I have used Int32? because it is nullable in my ORM for my database, so there is that.

4 Answers 4

6

It doesn't make any difference to the compiled code; int is synonymous with System.Int32 and string is synonymous with System.String.

However, using int and string etc. is more idiomatic.

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

2 Comments

There's no difference. String and Int32 are the actual type names in the .NET Framework. string and int are aliases for them defined by the C# language (probably to make it more like C/C++).
It's a product of the .NET Common Type System - also see msdn.microsoft.com/en-us/library/zcx1eb1e(VS.80).aspx
1

It's largely a stylistic issue -- the builtin type names like int are formally defined as aliases for standard library types like System.Int32.

Comments

0

'int' is simply an alias for "Int32". However, I have never seen any code that uses the aliased classes instead of the alias in C#. If I wanted to make damn sure that the int I was using was 32 bits I would use Int32, but I don't see a reason for using the more verbose "Boolean" instead of the standard alias, "bool".

5 Comments

It's so OCD for me it comes down to colors and case in Visual Studio. I know, it's insane.
@Ed: Even if you did want to make damn sure that an int was 32 bits then you could still use int rather than Int32. It's guaranteed in the C# and CLI specs. It won't change.
Luke: For people who don't know the spec by heart of in code where numerout different bit-nesses of integers are used I think it can actually make a readability difference.
Int32 can be useful when dealing with interop and binary issues. I used it e.g. when writing a binary format parser, just because it looked more consistent in the mix with Int16, UInt16 etc.
You don't need to know a spec by heart, to be honest - virtually every "C# for Dummies" kind of book I've seen has a table of primitive types with their keywords and ranges right in the first chapter.
0

i would stick to int, string, bool just incase i decide to port the code or part of the code to c/c++, it just makes it a little easier. also you would know that everybody knows what an int is bu they might not know that it's the same as Int32. even though they are basically the same i'd stick to int, string, bool.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.