When it matters, do you use int int Int32 or string versus String?

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

It usually doesn't matter if you use a keyword (e.g. int ) or an identifier (e.g. Int32 ) to refer to the built-in type. But there are exceptions for everything, so my question has two parts:

  • When does C # require you to use or not use a keyword?
  • When does using a keyword instead of an identifier change the value of a program?
+4
source share
8 answers

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

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

The base enumeration type must be specified using the keyword:

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

The expressions (x)+y , (x)-y and (x)*y interpreted differently depending on whether x keyword or 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 
+2
source

C # does not require you to use one or the other, as they are equivalent. This is personal preference and coding. So use one that seems more readable to you and your team. Only one tip: be consistent: don't use an alias in half of your code base and a fully qualified type name in the second half.

+8
source

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

+2
source

Identifiers int , string , bool , etc. are C # aliases for real types Int32 , string and Boolean respectively. It doesn't matter what you use, but when you write the API, some people prefer to use the actual class types for aliases.

Here's an MSDN article that lists equivalents.

+1
source

It does not matter. int is basically a shorthand for System.Int32 , they are almost perfectly interchangeable. Consider this #define if you want (but C # doesn't allow such #define).

0
source

One of the problems is that with strange using or String / Int32 member declarations, another type (i.e. not one of mscorlib) or even a different type of member may be displayed, which will lead to problems.

As John mentioned, another problem is that you cannot declare enumerations using an identifier (you get the error "Byte type, sbyte, short, ushort, int, uint, long or ulong expected").

0
source

It does not require you to use one or the other. However, if you want to increase or decrease int, you can explicitly use Int16 or Int64 instead of int, which is an alias for Int32.

0
source

The only situation I can think of in which you should use aliases in a form where you could use the class name is to determine the types of enums:

 public enum MyEnum:Byte //will not compile public enum MyEnum:byte //correct 

Note also that you must use the alias keyword to define an enumeration, while when defining code members that can be of any numbered type, you use the Enum class name.

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

0
source

Source: https://habr.com/ru/post/1389520/


All Articles