Monday, November 9, 2015

Const Vs ReadOnly

Const Vs ReadOnly: Both type variables used to hold constant values. Once we initialize them values can't be changed.

Const : Constant variables are compile time constants it means we need to initialize them before compiling the program.

ex:
 public const string aa = "";

readonly : Read only variables are run time constants, it means not required to initialize for compilation, later we can initialize them on run time, like initialize with some value retrieve from database, after initialization it can't be reassigned.

Ex:
public class test
    {
        public readonly static string bb;
        public test()
        {
            bb = Session["user"].Tostring();
        }
   
    }

------------------------------------------------------------------------
Is Vs As
------------ Is : This keyword is used to check data type of variable
As: This keyword is used to convert data type of variable

ex
    object str = "";
    if (str is object)
    {
        string obj = str as string;
    }


-------------------------------------------------------------------------
Debug keyword
--------------------- If we want to execute some code when running application in debug mode but want to skip that code from execution when running in release mode, this keyword is useful.
Ex.

Debug mode
# if DEBUG
        Console.WriteLine("hello");

#endif

Release mode
# if DEBUG
        Console.WriteLine("hello");
#endif


-------------------------------------------------------------------------
typeof Vs GetType
------------------------
typeof: It is used to retrieve type from class name
GetType: It is used to retrieve type from instance

Source Code:
    public class employee
    {

    }
    public class Student
    {

    }
   
   
employee objEmp = new employee();
   
   
if (objEmp.GetType() == typeof(Student))
    {
      Console.WriteLine("Matched...");
    }
    else
    Console.WriteLine("Not Matched...");


-------------------------------------------------------------------------
null-coalescing operator (??)
------------------------
null-coalescing operator is used to get not null value from a group of variable.
It assigns first not null variable value from a variable or a group of variable.

string s1;
string s2;
string s3 = "ram";
string s4 = "raj";

string res1 = s2 ?? "krisna";  // res1 will assigned 'krishna'
string res2 = s2 ?? s3;  //  res2  will assigned 'ram'
string res3 = s1 ?? s2 ?? s3; // res3 will assigned 'ram'

-------------------------------------------------------------------------
Portable class library
------------------------
It is portable class library, compatible for any type .net project.

No comments:

Post a Comment

CI/CD - Safe DB Changes/Migrations

Safe DB Migrations means updating your database schema without breaking the running application and without downtime . In real systems (A...