C# variables

C# variables

In C# a variable is a name given to a space in the computers memory that the program manipulates. The various types of variables decides the size,layout and the range of values for that space, as well as the operators that can be applied to it.

These types are separated into value types and reference types.(built in means that the types should be available in any class )

  • Value types:
    • Simple types :
      • Signed integral(sbyte,short, int, long);
      • Unsigned integral(byte, ushort, uint, ulong);
      • Unicode characters (char);
      • IEEE binary floating-point( float, double);
      • High-precision decimal floating-point (decimal);
      • Boolean( bool) -true or false ;
    • User defined types :Enum types (enum E { . . . });Struct types (Struct S { . . .})
    • Nullable value types:Extensions of all other value types with a null value
  • Reference types
    • Class types
      • Ultimate base class of all other types: object
      • Unicode strings: string
      • User-defined types of the form class C { . . .}
    • Interface types
      • User-defined types interface I {. . . }
    • Array types
      • Single- and multi-dimensional
    • Delegate types
      • User-defined types of the form delegate int D( . . . )

The variables I tend to use are string, int, double, float, char bool and the reference types. You can also use the key word var to make a variable, it has to be within a method though.

You declare variables by doing:

variableType variableName = variable;

so when declaring an int it would be something like:

int myNum =5:

You don’t have to declare a variable and assign it a value at the same time so long as you assign the value before you use it.

In which case you do:

variableType variableName;

If you don’t want anyone to be able to change the variable after its declaration(and you do need to declare the variable for it to work). You put const in front of the variableType so it would be:

const myNum=5;

which would cause an error if someone tried to overwrite the variable assignment later in the code. If you are just changing the assigned value just do:

variableName = Variable;

You can declare multiple variables of the same type at the same time by doing:

variableType varN1=v1, varN2=v2, varN3 = v3;

Note you can’t use key words as names for variables, and you want the name to be somewhat descriptive so you know what it is when you use it. The names are also case sensitive and should start with a lowercase letter (although I don’t think the lowercase part is required by the system), they can’t contain whitespace and I believe they can only use letters,numbers and the underscore character( _ ).

One thought on “C# variables

Comments are closed.