4

In C#, the keywords for built-in types are simply aliases for corresponding types in the System namespace.

Generally, it makes no difference whether you use a keyword (such as int) or an identifier (such as Int32) to refer to a built-in type. But there's an exception to everything, so my question in two parts is:

  1. When does C# require you to use, or not use, a keyword?
  2. When does using a keyword instead of an identifier change the meaning of the program?
Shog9
  • 156,901
  • 35
  • 231
  • 235
Michael Liu
  • 52,147
  • 13
  • 117
  • 150
  • I don't think this question is a duplicate of "C#, int or Int32? Should I care?" That other question asks why it might be preferable to use int instead of Int32. This question specifically asks about cases in which you *must* use int instead of Int32 (for example, in enums), or vice versa. The accepted answer to that question does not address this question. – Michael Liu Jan 05 '12 at 18:15
  • Your first question is answered by the duplicated question. Your second question is the wrong question, it makes no difference, unless one or the other is expected. For example when you create an enum, it can only be a byte, sbyte, short, ushort, int, uint, long, or ulong. – Security Hound Jan 05 '12 at 18:22
  • @Ramhound: In what way is the second question a "wrong question"? The "unless one or the other is expected" is exactly what the OP is asking: in which situations *is* one or the other expected? Also, it's certainly possible to have situations where both are valid but they give different results. – Jon Skeet Jan 06 '12 at 07:11
  • @JonSkeet: I'm reopening, so please consider answering this. –  Apr 02 '12 at 15:28

8 Answers8

8

C# doesn't require you to use one or the other as they are equivalent. It is a personal preference and coding convention. So use the one that seems more readable to you and your team. Just one advice: be consistent: do not use an alias in half of your codebase and the full type name in the second half.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Might also be worth being completely pedantic and to say that absent a `using System;` directive, you would have to use the built-in shorthands or fully qualify the types, as in `System.String val;` The shorthands work independently of using directives or full qualification, as they are built into the language. – Anthony Pegram Jan 05 '12 at 17:54
2

string is an alias for the type System.String, and int is an alias for System.Int32. Therefore, use to your preference.

2

A using alias directive cannot use a keyword as the type name (but can use keywords in type argument lists):

using Handle = int; // error
using Handle = Int32; // OK
using NullableHandle = Nullable<int>; // OK

The underlying type of an enum must be specified using a keyword:

enum E : int { } // OK
enum E : Int32 { } // error

The expressions (x)+y, (x)-y, and (x)*y are interpreted differently depending on whether x is a keyword or an identifier:

(int)+y // cast +y (unary plus) to int
(Int32)+y // add y to Int32; error if Int32 is not a variable
(Int32)(+y) // cast +y to Int32

(int)-y // cast -y (unary minus) to int
(Int32)-y // subtract y from Int32; error if Int32 is not a variable
(Int32)(-y) // cast -y to Int32

(int)*y // cast *y (pointer indirection) to int
(Int32)*y // multiply Int32 by y; error if Int32 is not a variable
(Int32)(*y) // cast *y to Int32
Michael Liu
  • 52,147
  • 13
  • 117
  • 150
  • Current C# allows enum E : Int32{}. No idea when it changed, but that code compiles fine in VS2019. – MichaelS Feb 03 '22 at 22:33
1

The identifiers int, string, bool, etc. are C# language aliases for the real types Int32, String, and Boolean, respectively. It doesn't matter which you use, but when you're writing an API some people prefer to use the actual class types to the aliases.

Here's an MSDN article that lists the equivalents.

Yuck
  • 49,664
  • 13
  • 105
  • 135
0

It doesn't matter. int is basically a shorthand for System.Int32 they are almost perfectly interchangable. Consider it a #define if you like (but C# doesn't allow #defines like that).

McKay
  • 12,334
  • 7
  • 53
  • 76
0

One issue is that with strange usings or member declarations String/Int32 can map to a different type(i.e. not the one from mscorlib) or even another kind of member, which would lead to problems.

As Jon mentioned, another issue is that you can't declare enums using an identifier(You get the error "Type byte, sbyte, short, ushort, int, uint, long, or ulong expected").

CodesInChaos
  • 106,488
  • 23
  • 218
  • 262
0

It doesn't require you to use one or the other. However, if you want a larger or smaller int you might want to explicitly use Int16 or Int64 instead of int which is an alias for Int32.

zaq
  • 2,571
  • 3
  • 31
  • 39
  • Not sure where your example is going - why would you not use `short` or `long`? However, C# *does* require you to use the keyword when declaring enums. – Jon Skeet Jan 05 '12 at 17:55
  • Actually, that's a good point. Alias's exist for Int16 and Int64 as well so I guess it's really up to the developer which to use. – zaq Jan 05 '12 at 18:02
0

The only situation I can think of in which you are required to use the aliases in a form where you might otherwise be able to use a class name is in the definition of enum types:

public enum MyEnum:Byte //will not compile

public enum MyEnum:byte //correct

Notice also that the definition of an enum requires use of the alias keyword, while when defining code members that can accept any enumerated type, you use the class name Enum.

Lastly, you can never specify System.ValueType as a base class or generic type parameter; you instead use the struct keyword, which is essentially an alias for an object deriving from ValueType.

KeithS
  • 70,210
  • 21
  • 112
  • 164