More about C# variables

Keywords: C#

Type conversion

In general, different types of variables use different patterns to represent data. This means that even if you move a series of bits from one type of variable to another type of variable, the result may be different from what you expect.

Therefore, data needs to be type converted instead of mapping data bits from one variable to another. There are two types of type conversion:

  • Implicit conversion: the conversion from type A to type B can be carried out in all cases. The rules for executing the conversion are very simple, and the compiler can execute the conversion.
  • Explicit conversion: the conversion from type A to type B can be carried out in some cases. The conversion rules are complex and some type of additional processing should be carried out.

Implicit conversion

Implicit transformation does not require any work or additional code.

var1 =var2;

If the type of var2 can be implicitly converted to the type of var1, this assignment statement involves implicit conversion.

Implicit numeric conversion
typeCan be safely converted to

The rule of implicit conversion is that any type A can be implicitly converted to type B as long as its value range is completely included in the value range of type B.

Explicit conversion

Explicit conversion is performed when the compiler is explicitly required to convert values from one data type to another. Therefore, this requires additional code, and the format of the code varies depending on the conversion method.


This conversion method is only feasible in some cases. Types that have little or no relationship with each other cannot be cast.

Complex variable types

In addition to these simple variable types, C# also provides three more complex variables:

  • enumeration
  • structure
  • array


Enumeration type allows you to define a type whose value range is a limited set of user provided values.

Enumeration can be defined using enum keyword:

enum <typeName>

Then declare the variable of this new type and assign a value

<typeName> <varName>;
<varName> = <typeName>.<value>;

By default, each value is automatically assigned to the corresponding basic type value according to the defined order.

enum <typeName> : <underlyingType>
  <value1> = <actualVall1>,
  <value2> = <actualVall2>,
  <value3> = <actualVall3>,
  <valueN> = <actualVallN>


Structure is a data structure composed of several data, which may have different types. According to this structure, you can define your own variable types.

Use the struct keyword to define the structure:

struct <typeName>

The < member declarations > section contains the declaration of variables (called data members of structures) in the same format as the previous variable declaration:

<accessibility> <type> <name>;

To let the code calling the structure access the data members of the structure, you can use the keyword public for < accessibility >.

struct route
  public orientation direction;
  public double      distance;

After defining a structure type, you can define the variables of the structure type:

route myRoute;


All the previous types have one thing in common; They all store only one value (a set of values in the structure). If you need to store a lot of data, it will be very inconvenient, and the array is an index list of variables, which are stored in array type variables.

int i
for (i = 0; i < 3; i++)
  WriteLine($"Name with index of {i}: {friendNames{i}");

Declaration array

Declare the array as follows:

<baseType>[] <name>;

< basetype > can be any variable type. The array must be initialized before accessing. There are two ways to initialize the array:

  • Specifies the full contents of the array as literals
  • Specify the size of the array and initialize all array elements with the keyword new

To specify an array with literals, simply provide a comma separated list of element values, which is placed in curly braces.

int[] myIntArray = { 5, 9, 10, 2, 99 };

Syntax of another method:

int[] myIntArray = new int[5];

foreach loop

foreach loops can use a simple syntax to locate each element in an array:

foreach (<baseType> <name> in <array>)
  // can use <name> for each element

Pattern matching using switch case expressions

In C# 7, you can perform pattern matching in switch case based on the type of variable. Because the type of a variable is known, you can access the methods and properties provided by that type.

switch (<testVar>)
   case int value:
     <code to execute if <testVar> == <comparisonVall> >
   case string s when s.Length == 0:
     <code to execute if <testVar> is a string with a length = 0 >
   case null:
     <code to execute if <testVar> == null>
     <code to execute if <testVar> != comparisonVals>

The case keyword is followed by the type of variable you want to check. When the case statement matches, the value of this type is saved to the declared variable. C# 7 applied the when keyword modifier to the switch case expression. The when keyword modifier allows you to extend or add some additional conditions to execute the code in the case statement.

Multidimensional array

A multidimensional array is an array that uses multiple indexes to access its elements.

The two-dimensional array is declared as follows:

<baseType>[,] <name>

  Multidimensional arrays only need more commas:

<baseType>[,,,] <name>;

Array of arrays

Sawtooth array, in which the number of elements in each row may be different. To do this, you need an array in which each element is another array. You can also have arrays of arrays, or even more complex arrays. Note that these arrays must have the same basic type.

When declaring an array of arrays, its syntax requires that multiple parenthesis pairs be specified in the declaration of arrays:

int[] [] jaggedIntArray;

Initializing such arrays is not as simple as initializing multidimensional arrays:

int[] [] jaggedIntArray = { new int[] { 1, 2, 3}, new int[] {1},
                            new int[] {1, 2} };

Posted by Permutant on Fri, 10 Sep 2021 13:10:51 -0700