Monday, 5 March 2018

008 C# Access Modifiers

Scope and Access Level :
Encapsulation is a process hiding member of an object from outside world. In object oriented 
programming, encapsulation play a big role to prevent access of an object. Encapsulation and  
abstraction are related to access level of an object. Both used to make object visibility
 implementation and hide the object from outside the world.
 
                         A member be visible from outside object and can be access from outside object as specified by the Access Level. Element of an application can be access according to the access modifier defined on the element. 


The following are the access modifier 
  1.  Public 
  2.  Private 
  3.  Protected 
  4.  Internal 
  5.  Protected internal.


The features of access modifier listed below. 


Public: Public modifier are visible to all code in the application.Can be access form anywhere in the program. Member of the same type access any public modifier, public type can be derived. Public type can be visible are inside the same assembly only. Here is an example of public type . 


Example 1


public class Student
{
             int i = 56;
            public int getData()
            {
             return 5;
             }
}


Student obj = new Student();
obj.getData();//access to pubcli class


Example 2
public struct college
{
               int l = 56;
               int k = 70;
               public int getData(int a1, int a2)
              {
                  l = a1+a2;
                   return l;
              }
}
 
college obj = new college();
obj.getData(10,20);//access to pubcli struct


Private : Private class are access only by the same member same member of a type. Private type cannot be derived. No outside program can we access to the private type modifier. Private type modifier cannot be access from outside assembly. Here is the example of private type modifier. 


 Example 1


private class Student
{
               int i = 56;
              private getData()
             {
              return 5;
             }
}
Student obj = new Student();
obj.getData();//error


Example 2


private struct college
{
 int l = 56;
  int k = 70;
  private int getData(int a1, int a2)
     {
     l = a1+a2;
     return l;
    }
}
college obj = new college();
obj.getData();//error






Protected: Protected type modifier can we access from within the type define in it and the type derived from it. Protected class can not be access from outside the class rather than derived class. Protected class cannot be  access from outside the Assembly. Here is the example of protected access modifier. 


 Example 1


protected class Student
{
          int i = 56;
         public int getData()
         {
           return 5;
          }
}


protected class Student_child : Student
{
          int l = 60;
          public int getData_child()
          {
          return 5;
         }
}


Student obj = new Student();
obj.getData();//


Student_child obj1 = new Student_child();
obj1.getData_child();//
obj1.getData();//method of student
 
 Example 2
protected struct college
{
         int l = 56;
         int k = 70;
         public int getData(int a1, int a2)
        {
          l = a1+a2;
          return l;
        }
}


Internal: Internal is access modifier ,which tells the compiler, that type will be accessible from all within the Assembly but not accessible outside the Assembly. Internal also be accessed from within the type define in it. Here is the example of internal class. 


 Example 1


internal class Student
{
}




Protected Internal : Protected internal access modifier can we access from within the tribe drive in it, access form derived class and visible within the Assembly only. 


protected class Student
{
            protected internal string Name;
           protected internal decimal price;
}






Student obj = new Student();
obj.Name = "John";
obj.price = 20;


Here are some interview question. 


1)Can you declare a private class in a namespace? 


           A class in a namespace are by default internal, that means that class can be visible could assembly within this namespace. The class can be declared public only not private protected internal. It will throw error. But if we are declaring a class with nested type, the nested type can be declared as private class. 


2)What are the default access modifier of a class? 


 All class defined in a. Net application or internal by default.

025 C# Namespace

Namespace defines the area of code. Namespace separate objects in different area. In each area, there are some object define in it. Each area is different from other and namespace prevent conflict between area.
 
        To use a particular object, we need to add the namespace  to get this object. Namespace also allowed  hierarchy. One  namespace can be define  under another  namespace.



The syntax of the namespace define below. Namespace is a keyword and one opening and closing bracket is necessary. In general, namespace content classes , but . Net architecture allow interface ,enumeration, deligate, structure etc.


 namespace namespace_name
{
   // class,structure ,enumeration   etc. 
}



Object under namespace can be accessed by using. First namespace keyword , then "." , then  namespace name.



namespace_name.object;
 
Below is a example of the same. Below is a program, to show how a namespace is used in a program.



using System;
namespace Student1namespace
{
        class School1
         {
              public void ShowName()
               {
                   Console.WriteLine("You are in School1 namespace");
               }
         }
 
namespace Student2namespace
{
            class School2
           {
                public void ShowName()
                {
                  Console.WriteLine("You are in School2 namespace");
               }
          }
}

class Program
{
         static void Main(string[] args)
          {
                Student1namespace.School1 fc = new Student1namespace.School1();
                Student2namespace.School2 sc = new Student2namespace.School2();
                fc.ShowName();
                sc.ShowName();
                Console.ReadKey();
         }
}



Output:
You are in School1 namespace
You are in School2 namespace




using keyword: using keyword declared at the top of the program followed by namespace name. You can use multiple using statement with multiple namespace. This will result, each object of the mention namespace will be available throughout the program. Syntax of the using statement is here



for example so now we have a namespace named "Student1namespace ". Here is the example,  how this can be use.



using System;
using Student1namespace;
using Student2namespace;

namespace Student1namespace
{
        class School1
        {
           public void ShowName()
            {
               Console.WriteLine("You are in School1 namespace");
            }
        }
 
namespace Student2namespace
{
         class School2
        {
            public void ShowName()
            {
              Console.WriteLine("You are in School2 namespace");
            }
        }
}

class Program
{
       static void Main(string[] args)
       {
           School1 fc = new School1();
             School2 sc = new School2();
             fc.ShowName();
             sc.ShowName();
             Console.ReadKey();
        }
}
 



Output:
You are in School1 namespace
You are in School2 namespace 







Nested namespace : To namespace can be declared under another namespace. The innermost namespace object, you should use. Operator to access the hierarchy. The outermost namespace, followed by inner namespace, then the object name.



using System;

namespace Student1namespace
{
         class School1
         {
               public void ShowName()
               {
               Console.WriteLine("You are in School1 namespace");
              }
         }

      namespace Student2namespace
     {
           class School2
          {
              public void ShowName()
              {
                Console.WriteLine("You are in School2 namespace");
              }
          }
    }

}


class Program
{
      static void Main(string[] args)
      {
               Student1namespace.Student2namespace.School2 fc = new         Student1namespace.Student2namespace.School2();
               fc.ShowName();
              Console.ReadKey();
     }
}



Output:
You are in School2 namespace


Namespace Alias : you can use in namespace as alias. For example, a namespace "myNamespace"  can be alias   as "ab". Below is the syntax of the same.



 ab=myNamespace



Here is example of, how a namespace can be alias and use in program.






using System;
using alias1=Student1namespace;
using alias2 = Student2namespace;

namespace Student1namespace
{
        class School1
        {
             public void ShowName()
              {
                Console.WriteLine("You are in School1 namespace");
             }
      }
}

namespace Student2namespace
{
          class School2
          {
              public void ShowName()
              {
                Console.WriteLine("You are in School2 namespace");
              }
         }
}


class Program
{
         static void Main(string[] args)
         {
             alias1.School1 fc = new alias1.School1();
             alias2.School2 sc = new alias2.School2();
             fc.ShowName();
             sc.ShowName();
            Console.ReadKey();
       }
}

Global Namespace : System is the root of all namespace. System contain base class and fundamental class generally commonly used. All namespace is comes under the system namespace. Now if I define a class named "system".The following you will be happen.

namespace MyNamespace
{
             class Student
             {
                    public class System { }
                    static void Main(string[] args)
                       {
                          System.Console.WriteLine("Hello, World!");
                          System.Console.ReadKey();
                       }

            }
}

error Output :
 'Student.System' does not contain a definition for 'Console'

"Console" comes under the System namespace , But system class is defined locally, it will super sit  System namespace. System namespace and function and will not be available . In this situation, belowe "global" keyword is used to identify the Global namespace. Is example of the same.

namespace MyNamespace
{
    class Student
      {
          public class System { }
          static void Main(string[] args)
          {
               global::System.Console.WriteLine("Hello, World!");
               global::System.Console.ReadKey();
          }

    }
}

বাঙালির বেড়ানো সেরা চারটি ঠিকানা

  বাঙালি মানে ঘোড়া পাগল | দু একদিন ছুটি পেলো মানে বাঙালি চলল ঘুরতে | সে সমুদ্রই হোক , পাহাড়ি হোক বা নদী হোক। বাঙালির ...