Part 3 - C# Tutorial - Built-in data-types

Watch before proceed 

we will discuss

  1. Value Type
  2. Reference types
  3. Types of value type and Reference Type

Variables of value types directly contain the data inside variables(reference) to their data,
ex- 
           int a=24;

Value types : 

Value Data Types will directly store the variable value,Derived class for these data types are System.ValueType are further divided into simple types, enum types, struct types,and nullable value types.

Simple Type

Signed integral: sbyte, short, int, long
Unsigned integral: byte, ushort, uint, ulong
IEEE binary floating-point: float, double

High-precision decimal floating-point: Decimal 

    2  Enum Types is a value type defined by a set of named constants of the underlying integral numeric type.To define an enumeration type, use the enum keyword and specify the names of enum members

    ex-


    enum Season
    {
    spring,
    summer,
    winter
    }

    3 Structs Structs are mainly useful to hold small data values. we will discuss later next tutorial.
    example-
    struct Employee
    {
        public int EmpId;
        public string FirstName;
        public string LastName;
    }

    4 Nullable can't be assigned a null value.ex- int i=null;You can declare nullable type using Nullable<t> where T is a Type. Nullable <int> i = null;can be assigned any value from -2147483648 to 2147483647, or a null value.

    Example -
    static void Main(string[] args)
    {
        Nullable<int> i = null;
    
        if (i.HasValue)
            Console.WriteLine(i.Value); // or Console.WriteLine(i)
        else
            Console.WriteLine("Null");
    }

    Syntax :

    You can use the '?' operator to shorthand the syntax e.g. int?, long? instead of using Nullable <T>.
    double? pi = 3.14; char? letter = 'a'; // An array of a nullable type: int?[] arr = new int?[10];
    static void Main(string[] args)
    {
        int? i = null;
    
        int j = 10;
    
    
        if (Nullable.Compare<int>(i, j) < 0)
            Console.WriteLine("i < j");
    
        else if (Nullable.Compare<int>(i, j) > 0)
    
            Console.WriteLine("i > j");
    
        else
            
    Console.WriteLine("i = j");
    
    } 
    Reference types :

    are further divided into class types, interface types, array types, and delegate types.won’t store the variable value directly in memory. The built-in reference types are string, object.

    Reference Type 

    The reference types do not contain the actual data stored in a variable, but they contain a reference to the variables.In other words, they refer to a memory location. Using multiple variables, the reference types can refer to a memory location. If the data in the memory location is changed by one of the variables, the other variable automatically reflects this change in value. 

    Example of built-in reference types areobjectdynamic, and string.

    1. Object Type :

     The Object Type is an alias for System.Object class. The object types can be assigned values of any other types, value types, reference types,  predefined or user-defined types. However, before assigning values, it needs type conversion.

    BOXING

    The process of Converting a value Type (char, int etc.) to a Reference Type(object) is called Boxing.The Value type is always stored in Stack. The Referenced Type is stored in Heap.

    example:

     static public void Main()
        {
    // assigned int value
                         int i  = 24;
    // boxing
                         object  o = i;
    Console.WriteLine ("Type of vaue" o);
    }

    1.  The value stored on the stack copied to the object stored on the heap memory.

    2.  Boxing is an implicit conversion process.

    Unboxing:- The Process of Converting a Reference Type(object) to Value Type is called Un-
    boxing.

    example:

     static public void Main()
        {
    // assigned int value
                         int i  = 24;
    // boxing
                         object  o = i;
                       //unboxing
                      int a = int (o);
    Console.WriteLine ("Type of vaue" a);
    }

    1.  the object stored on the heap memory copied to the value stored on the stack .

    2.  Boxing is an implicit conversion process.

    2. Dynamic Type

    Introduced in C# 4.0.It is used to avoid the compile-time type checking. The compiler does not check the type of the dynamic type variable at compile time.We can define this data type using the keyword “dynamic" .and Intellisense is not available in Dynamic Type.

    No need to initialize at the time of declaration.

    Example: 
                        dynamic str;
                str="I am a string"; //Works fine and compiles
                str=2; //Works fine and compiles

    Example:
                                 
                     dynamic obj1=1;
                     obj1="I am a string";
                  

    will compile and run since the compiler creates the type for obj1 as System.Int32 and then recreates the type as string when the value "I am a string" was assigned to it.
    This code will work fine.

    String Type :

    This represents a sequence of zero or more Unicode character . 'string' and 'String' is keyword is an alias System.String.String are immutable -the contents of a string object can't be changed after the object is created and It can contain nulls.

    example:-A string "modification" is actually a new string creation, you must use caution when you create references to strings. If you create a reference to a string, and then "modify" the original string, the reference will continue to point to the original object instead of the new object that was created when the string was modified. The following code illustrates this behavior:

    // Concatenate s1 and s2. This actually creates a new

    // reference to the original object.

                    string s1 = "Hello ";
                   string s2 = s1;
                   s1 += "World";

                 System.Console.WriteLine(s2);
                  //Output: Hello

    If you can facing any c sharp concept that you are looking for some more, please leave a comment & Email, I will have it added as soon as I can.