csharp_types_variables.md - brainchildservices/curriculum GitHub Wiki

SLIDE-1&2

  • Csharp Variables

    • Variables are containers for storing data values.

    • A variable is a name given to a storage area that is used to store values of various data types.

    • A variable is nothing but a name given to a storage area that our programs can manipulate.

    • Each variable in C# has a specific type, which determines:

      • the size and layout of the variable's memory
      • the range of values that can be stored within that memory
      • the set of operations that can be applied to the variable.
    • C# also allows defining other value types of variable such as enum and reference types of variables such as class, which we will cover in subsequent chapters.

    • Declaring (Creating) Variables

      • To create a variable, you must specify the type and assign it a value:

                   type variableName = value;
        
      • Where type is a C# type (such as int or string), and variableName is the name of the variable (such as x or name). The equal sign is used to assign values to the variable.

    • To create a variable that should store text, look at the following example:

                    string name = "John";
                    Console.WriteLine(name);
      
    • To create a variable that should store a number, look at the following example:

                    int myNum = 15;
                    Console.WriteLine(myNum);
      
    • You can also declare a variable without assigning the value, and assign the value later:

                    int myNum;
                    myNum = 15;
                    Console.WriteLine(myNum);
      

SLIDE-3,4&5

Csharp Datatypes

  • A data type specifies the size and type of variable values. It is important to use the correct data type for the corresponding variable to avoid errors, to save time and memory, but it will also make your code more maintainable and readable.
  • As explained in the variables chapter, a variable in C# must be a specified data type
    • Value types

      • A data type is a value type if it holds a data value within its own memory space.

      • It means the variables of these data types directly contain values.

      • The following data types are all of value type:

        • bool
        • byte
        • char
        • decimal
        • double
        • enum
        • float
        • int
        • long
        • sbyte
        • short
        • struct
        • uint
        • ulong
        • ushort
      • Passing Value Type Variables

        • When you pass a value-type variable from one method to another, the system creates a separate copy of a variable in another method.
        • If value got changed in the one method, it wouldn't affect the variable in another method.
    • Reference Types

      • Unlike value types, a reference type doesn't store its value directly. Instead, it stores the address where the value is being stored. In other words, a reference type contains a pointer to another memory location that holds the data.

      • The followings are reference type data types:

        • String
        • Arrays (even if their elements are value types)
        • Class
        • Delegate
      • Passing Reference Type Variables

        • When you pass a reference type variable from one method to another, it doesn't create a new copy; instead, it passes the variable's address.
        • So, If we change the value of a variable in a method, it will also be reflected in the calling method.

SLIDE-6&7

  • C# Null
    • The default value of a reference type variable is null when they are not initialized. Null means not refering to any object.
    • A value type variable cannot be null because it holds value, not a memory address.
    • C# 2.0 introduced nullable types, using which you can assign null to a value type variable or declare a value type variable without assigning a value to it.