Revision

Back to C# - Types


Built-in Types

Built-in Value Types

  bool b = true;
  int i = 1;
  int minInt = Int32.Min;
  int maxInt = Int32.Max;
  float x = 0.1F; // type Single
  Single s = 0.1F; // type Single
  double y = -1.2D;
  decimal z = 2.3M;
  char c = "A";


Difference among Numerical types

Difference of Interger types
type Range Size
sbyte -128 to 127 Signed 8-bit integer
byte 0 to 255 Unsigned 8-bit integer
short -32,768 to 32,767 Signed 16-bit integer
ushort 0 to 65,535 Unsigned 16-bit integer
int -2,147,483,648 to 2,147,483,647 Signed 32-bit integer
uint 0 to 4,294,967,295 Unsigned 32-bit integer
long -2^63 to 2^63-1 Signed 64-bit integer
ulong 2^64-1 Unsigned 64-bit integer


Difference of Decimal types

float is an alias for Single (float and Single are hence equivalent) and double are floating binary point types whereas decimal is a floating decimal point type.

type Range Size
float 7 digits 32-bit
double 15-16 digits 64-bit
decimal 28-29 digits 128-bit
  float flt = 1F/3;
  double dbl = 1D/3;
  decimal dcm = 1M/3;
  Console.WriteLine("float: {0} double: {1} decimal: {2}", flt, dbl, dcm);

Result:

float: 0.3333333  
double: 0.333333333333333  
decimal: 0.3333333333333333333333333333


Declare Value Type

One can create value types using the keyword struct.


Built-in Reference Types

  string s = "Hello World!";

  Console.WriteLine(s.Length); // 12 - Size of string

  int index = 1;
  Console.WriteLine(s[index]); // e - Access string letters by index

  int indexStart = 6, indexEnd = 10;
  Console.WriteLine(s[indexStart..indexEnd]); // World - Slice string by index (indexEnd excluded)

  Console.WriteLine(s.IndexOf("e"));  // 1 - Find index of char
  Console.WriteLine(s.IndexOf("World"));  // 6 - Find indexes of subString


Declare Reference Type

The following keywords can be used to declare reference types:


Untyped variable creation

var vs dynamic

var dynamic
var untyped variables are created using the keyword var dynamic untype variables are created using the keyword dynamic
Type of var variable is automatically determined by the compiler at compilation time from the value used to initialize the variable Type of dynamic variable is determined at runtime
Initialization is mandatory for a var variable in order to know its type at compilation time Initialization is not mandatory for a dynamic variable
A var variable can’t change type after it has been created A dynamic variable can change type after it has been created
var can’t be used to return values from a function or for properties dynamic can be used to return values from a function or for properties

var

  var u = 1; // u will be typed integer at compilation time
  var v = true; // v will be typed bool at compilation time
  var w = "Untyped variable"; // w will be typed string at compilation time

This will not work:

  var e = 1;
  e = "test"; // ERROR: it is not an integer

This neither:

  var f; // ERROR: cannot implicitly cast a to a type


dynamic

  dynamic u = 1; // u will be typed integer at runtime
  dynamic v = true; // v will be typed bool at runtime
  dynamic w = "Untyped variable"; // w will be typed string at runtime

  dynamic e; // e will wait an assignation of data to have type
  e = 1; // e has now the type int

  dynamic f = 1;
  f = "test"; // f will change type from int to string


Nullable

You can make a value type nullable (make it accept null values) by adding a ? after the type name (reference type can be null without using the ?):

  int? i = null;
  float? x = null;


Constant

Constants are immutable values which are known at compile time and do not change for the life of the program.

  const int valConst = 1;

  class MyClassConst
  {
      const int constVar = 1;
  }

const vs readonly

In a class or a struct, const or readonly can be used. Here are their main differences:

const readonly
Defined at compile time Defined at compile time or runtime
Implicitly static Instance-level or static
In a class or a struct, defined at declaration In a class or a struct, defined at declaration or in the constructor
  class MyClassConst
  {
      const int constVar = 1; // can't be define in constructor
  }

  class MyClassReadOnly
  {
      const int readOnlyVar; // can be define in constructor

      public MyClassReadOnly(int roVar) {
        this.readOnlyVar = roVar; // use of 'this' is not mandatory
      }
  }


Ressources

See: