Sunday, 8 December 2013

Access Specifiers in C#.Net

What is Access Specifiers in C#?
Access Specifiers defines the scope of a class member. A class member can be variable or function. In C# there are five types of access specifiers are available.



The class member, that is defined as public can be accessed by other class member that is initialized outside the class. A public member can be accessed from anywhere even outside the namespace.
Example:
using System;

namespace Public_Access_Specifiers
{
  class access
   {
     // String Variable declared as public
     public string name;
     // Public method
     public void print()
      {
        Console.WriteLine("\nMy name is "+name);
      }
   }

  class Program
   {
     static void Main(string[] args)
      {
        access ac = new access();
        Console.Write("Enter your name:\t");
        // Accepting value in public variable that is               outside the class
        ac.name = Console.ReadLine();
        ac.print();

        Console.ReadLine();
      }
   }
}


The private access specifiers restrict the member variable or function to be called outside from the parent class. A private function or variable cannot be called outside from the same class. It hides its member variable and method from other class and methods. However, you can store or retrieve value from private access modifiers using get set property. You will learn more about get set property in lateral chapter.
Example:

using System;

namespace Private_Access_Specifiers
{
  class access
   {
     // String Variable declared as private
     private string name;
     public void print() // public method
      {
        Console.WriteLine("\nMy name is " + name);
      }
   }

  class Program
   {
     static void Main(string[] args)
      {
        access ac = new access();
        Console.Write("Enter your name:\t");
        // raise error because of its protection level
        ac.name = Console.ReadLine();
        ac.print();
        Console.ReadLine();
      }
   }
}


The protected access specifier hides its member variables and functions from other classes and objects. This type of variable or function can only be accessed in child class. It becomes very important while implementing inheritance.
Example:

using System;
 
namespace Protected_Specifier
{
  class access
   {
     // String Variable declared as protected
     protected string name;
     public void print()
      {
        Console.WriteLine("\nMy name is " + name);
      }
   }
 
  class Program
   {
     static void Main(string[] args)
      {
        access ac = new access();
        Console.Write("Enter your name:\t");
        // raise error because of its protection level
        ac.name = Console.ReadLine();
        ac.print();
        Console.ReadLine();
      }
   }
}


The internal access specifier hides its member variables and methods from other classes and objects, that is resides in other namespace. The variable or classes that are declared with internal can be access by any member within application. It is the default access specifiers for a class in C# programming.
Example:

using System;
 
namespace Internal_Access_Specifier
{
  class access
   {
     // String Variable declared as internal
     internal string name;
     public void print()
      {
        Console.WriteLine("\nMy name is " + name);
      }
   }
 
  class Program
   {
     static void Main(string[] args)
      {
        access ac = new access();
        Console.Write("Enter your name:\t");
        // Accepting value in internal variable
        ac.name = Console.ReadLine();
        ac.print();
        Console.ReadLine();
      }
   }
}



The protected internal access specifier allows its members to be accessed in derived class, containing class or classes within same application. However, this access specifier rarely used in C# programming but it becomes important while implementing inheritance.
Example:

using System;
 
namespace Protected_Internal
{
  class access
   {
     // String Variable declared as protected internal
     protected internal string name;
     public void print()
      {
        Console.WriteLine("\nMy name is " + name);
      }
   }
 
 
  class Program
   {
     static void Main(string[] args)
      {
        access ac = new access();
        Console.Write("Enter your name:\t");
        // Accepting value in protected internal variable
        ac.name = Console.ReadLine();
        ac.print();
        Console.ReadLine();
      }
   }
}

Partial class C#.Net

Introduction
A Partial class is one that can be split among multiple physical files. This feature was introduced with the release of C# version 2.0. With C#, we can split the source code for a class into separate files so that we can organize the definition of a large class into smaller, easier to manage pieces. When we split a class across multiple files, we define the parts of the class by using the partial keyword in each file. Each file contains a section of the class definition, and all parts are combined when the application is compiled. Look at the example below:

Original and Single Class File (Calculation.cs)
class ClassRoom
{
    
private int boycount;   //field
    public ClassRoom()     //default constructor
    
{
        
boycount = 30;
    
}
    public ClassRoom(int bcount)     //overloaded constructor
    
{
        
boycount = bcount;
    
}
    public double Avg()     //method
    
{
        
//statements goes here
    
}
}

Splitted Class Files into two parts
//Calculation1.cs
partial class ClassRoom
{
    
private int boycount;   //field

    
public ClassRoom()     //default constructor
    
{
        
boycount = 30;
    
}
}
//Calculation2.cs
partial class ClassRoom
{
    
public ClassRoom(int bcount)     //overloaded constructor
    
{
        
boycount = bcount;
    
}
    public double Avg()     //method
    
{
        
//statements goes here
    
}
}
Now, when we compile a class that has been split into separate files, we must provide all the files to the compiler.
Partial Classes Rules and Advantages
To work on large projects, spreading a class over separate files allows multiple programmers to work on it simultaneously.
When working with automatically generated source, code can be added to the class without having to recreate the source file.
To split a class definition, use the partial keyword modifier.
All the parts must have the same accessibility, such as public, private, and so on.
The partial modifier can only appear immediately before the keywords class, struct, or interface.