Variables in C#

 In C# all variables must be declared before they can be used.

<datatype> <name of variable> = <value>;

Variable types:

  • Local variables 
  • Instance or non-static variables
  • Static or Class variables
  • Constant variables 
  • Read-only variables

Atomicity of variable references: Reads and writes of the following data types shall be atomic: bool, char, byte, sbyte, short, ushort, uint, int, float, and reference types. In addition, reads and writes of enum types with an underlying type in the previous list shall also be atomic. Reads and writes of other types, including long, ulong, double, and decimal, as well as user-defined types, need not be atomic. Aside from the library functions designed for that purpose, there is no guarantee of atomic read-modify-write, such as in the case of increment or decrement.


*to initialize a float value, postfix it with f, e.g. float myFloatVar = 0.30f;
  • We need this f because by default C# takes the values as double
  • The double precision vs single precision in floating point would mean a loss of precision if an implicit double to float conversion were to occur. Therefore, a compile time check lets you know that you want a float and suffix it with a f


Comments