Thursday, 4 June 2015

MODIFIER CONST, READONLY,STATIC


-Const
Lets us look at example
Ex 1
namespace Sample                                                                         {
    public class MyClass
    {
        private const int a;

    }
    public class Sample2
    {
        static void Main()
        {       
        }
    }
}
// Outpur Error 1 A const field requires a value to be provided.

Const variable has to be initialized at the time of declaration and can’t be modified later.

Private static const int a=9; // Const can’t be marked as static

Ex 2
namespace Sample
{
    public class Sample1
    {
        private const Sample2 sample2 = new Sample2();//Error
      
    }
    public class Sample2
    {
   
    }
    public class Sample3
    {
        static void Main()
        {   
        }
    }
}
//Output  Error 1. A const field of a reference type other than string can only be initialized with null.

 A const field of a reference type other than string can only be initialized with null

Ex 3
namespace Sample
{
    public class Sample1
    {
        private const Sample2 sample2 = null;
      
    }
    public class Sample2
    {
   
    }
    public class Sample3
    {
        static void Main()
        { 
        }
    }
}

// Output No error

Ex4
namespace Sample
{
    public class Sample1
    {
        public const int a = 1;
        public const int b = b + 2;
    }
    public class Sample3
    {
        static void Main()
        {          
        }
    }
}
Value of b evaluates at compile time, A constant can participate in a constant expression.

-Static Field
Example 1
namespace Sample
{
    public class Sample1
    {
        public static int c1;
        static void Main()
        {
            Console.WriteLine(c1);
            Console.ReadKey();
        }
    }
}
//Output 0;
Static variable are initialized as soon as class load with the default value.                                            A variable in C# can never have an uninitialized value.
-ReadOnly
namespace Sample
{
    public class Sample1
    {
        public static readonly int a=8;
        static void Main()
        {
            a = 10;
            Console.WriteLine(a);
            Console.ReadKey();
        }
    }
}
//Output   Error 1 A static readonly field cannot be assigned to (except in a static constructor or a variable initializer)
Readonly field can be declared either at time of declaration or  in constructor of same class.

Difference between const and Readonly keyword
A const field can only be initialized at the declaration of the field.                                                A readonly field can be initialized either at the declaration or in a constructor.     Therefore, readonly fields can have different values depending on the constructor used.            
Const field is a compile-time constant whereas Readonly field can be used for runtime constant.                                                                                                                                                
namespace Sample
{
    public class Sample1
    {
        public  readonly int _a; //but const field shows error if field not assigned
        public Sample1(int a)
        {
            _a = a;
        }
        static void Main()
        {
            Sample1 sample = new Sample1(5);
           
            Console.WriteLine(sample._a);
            Console.ReadKey();
        }
    }
}
//Output  5      


Wednesday, 27 May 2015

ACCESS SPECIFIER (ACCESS MODIFIER)

Access Specifier(Access Modifier)  are keywords in object oriented programming,which specify accessibility of Types and Types Members. 

Remember these points:


  • By default Members of classes are  Private  .
  • By default Classes  are Internal.
  • By default Namespaces are Public but we are not supposed to specify public keyword.                                                 

Access Specifier are as follows:

1.Private -Members can be accessed within the class only                                                                                
********************Example 1********************************                                                     namespace Sample
{

   public class ABC
   {
       private int Id;
   }
 
    public class Program
   {
        public static void Main(string[] args)
        {
            ABC abc = new ABC();
            abc.Id = 10;//Error ,Id is private
        }
   }
}
// Output :Error 1 ‘Sample.ABC.Id’ is inaccessible due to its protection level 

2.Public- As name says members can be accessed from any class and any assembly.  
3:Protected-Members can be accessed within its class and derived class of same assembly
Protected members are also accessible outside assembly provided it should be derived 

.********************Example1********************************  
namespace Sample
{

   public class ABC
   {
       protected int Id;
   }
 
    public class Program
   {
        public static void Main(string[] args)
        {
            ABC abc = new ABC();
            abc.Id = 10;//error ,its protected
        }
   }

}

// Output :Error 1 ‘Sample.ABC.Id’ is inaccessible due to its protection level

********************Example2********************************
namespace Sample
{

    public class ABC
    {
        protected int Id;
    }
    public class XYZ : ABC
    {
        public void Print()
        {
            Id = 5;
            Console.WriteLine(Id);
        }
    }
    public class Program
    {
        public static void Main(string[] args)
        {
            XYZ xyz = new XYZ();
            xyz.Print();
            Console.ReadLine();
        }

    }
// Output : 5   

4:Internal- Members can be accessed only within the same assembly.      
Create a Class library

namespace ClassLibrary1
{
    public class Class1
    {
        internal int a;
    }
}
Now create a console application, take a reference of above library   
namespace Sample
{ 
    public class Program
   {
        public static void Main(string[] args)
        {
            ClassLibrary1.Class1 cls = new Class1();
            cls.a = 6; // error           
        }
   }
}
// Output  Error      1 'ClassLibrary1.Class1' does not contain a definition for 'a' and no extension method 'a' accepting a first argument of type 'ClassLibrary1.Class1' could be found (are you missing a using directive or an assembly reference?       

5:Protected Internal- Members can be accessed anywhere in same assembly and also accessible  by inheriting that class. It can be accessible outside assembly in derived class only. Protected Internal member works as Internal within the same assembly and works as Protected for outside the assembly.   

namespace ClassLibrary1
{
    public class Class1
    {
      protected internal int a;
    }
}
Now create a console application, take a reference of above library   
namespace Sample
{

   public class ABC:Class1
   {
       private int Id;
       public void XYZ()
       {
           a = 5;
           Console.WriteLine(a);
       }
   }
 
    public class Program
   {
        public static void Main(string[] args)
        {
            ABC abc = new ABC();
            abc.XYZ();
            Console.ReadKey();
          

        }
   }
}                     
// Output : 5