views:

90

answers:

4

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.

+5  A: 

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.

Greg Beech
What's the difference then? (I updated the question)
craigmoliver
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++).
Greg Beech
It's a product of the .NET Common Type System - also see http://msdn.microsoft.com/en-us/library/zcx1eb1e(VS.80).aspx
John K
+1  A: 

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

Jeffrey Hantin
A: 

'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".

Ed Swangren
It's so OCD for me it comes down to colors and case in Visual Studio. I know, it's insane.
craigmoliver
@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.
LukeH
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.
Joey
`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.
0xA3
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.
Pavel Minaev
A: 

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.

harryovers