22-12-2010
2. Private: If we declare a class or class member access modifier as private which can be accessed by only current class members.
Eg: For private access modifier:
namespace privateexample
{
Public class class1
{
Public int a =25;
Public void method1()
{
Console.WriteLine("Method1 value is " +a);
}
}
Class program
{
Void main()
{
Class1 c1obj1 = new class1();
c1obj1.method1();
Console.ReadLine();
}
}
}
Output: Method1 value is 25
Protected: If we declare a class member access modifier as protected which can be accessed by current class members & derived class members.
Eg: For Protected:
namespace protectedexample
{
Public class class1
{
Public int a = 25;
Public void method1()
{
Console.WriteLine("Method1 value is " +a);
}
}
Public class class2 : class1
{
Public void method2()
{
Console.WriteLine("Method2 value is " +a);
}
}
Class program
{
Void main()
{
Class2 c2obj1 = new class2();
c2obj1.method1();
c2obj1.method2();
Console.ReadLine();
}
}
}
Output: Method1 value is 25
Method2 value is 25
Internal: If we declare a access modifier of a class or class member as internal which can be accessed by all the class of current project.
Eg: For Internal:
namespace publicexample
{
Public class class1
{
Public int a = 25;
Public void method1()
{
Console.WriteLine("Method1 value is " +a);
}
}
Public class class2
{
Class1 c1obj = new class1();
Public void method2()
{
Console.WriteLine("Method2 value is " +a);
}
}
Class program
{
Static void main()
{
Class1 c1obj1 = new class1();
Console.WriteLine("a value is " +c1obj1.a);
c1obj1.method1();
Class2 c2obj1 = new class2();
c2obj1.method2();
Console.ReadLine();
}
}
}
Output: a value is 25
Method1 value is 25
Method2 value is 25
Protected Internal: If we declare a class or class member access modifier as a protected internal which can be accessed by all the classes of current project as well as derived classes from other projects within that application.
Constructors & Destructors:
Constructor: Constructor is a member of a class it will invoke automatically when an object is creative.
Constructor name same as class name with in the constructor we will write the initialization code.
Constructor will not return any value due to that reason it will not have the return types.
Syntax to define Constructor:
<access modifier> <classname()>
{
// Initialization code
}
Eg: For private access modifier:
namespace privateexample
{
Public class class1
{
Public int a =25;
Public void method1()
{
Console.WriteLine("Method1 value is " +a);
}
}
Class program
{
Void main()
{
Class1 c1obj1 = new class1();
c1obj1.method1();
Console.ReadLine();
}
}
}
Output: Method1 value is 25
Protected: If we declare a class member access modifier as protected which can be accessed by current class members & derived class members.
Eg: For Protected:
namespace protectedexample
{
Public class class1
{
Public int a = 25;
Public void method1()
{
Console.WriteLine("Method1 value is " +a);
}
}
Public class class2 : class1
{
Public void method2()
{
Console.WriteLine("Method2 value is " +a);
}
}
Class program
{
Void main()
{
Class2 c2obj1 = new class2();
c2obj1.method1();
c2obj1.method2();
Console.ReadLine();
}
}
}
Output: Method1 value is 25
Method2 value is 25
Internal: If we declare a access modifier of a class or class member as internal which can be accessed by all the class of current project.
Eg: For Internal:
namespace publicexample
{
Public class class1
{
Public int a = 25;
Public void method1()
{
Console.WriteLine("Method1 value is " +a);
}
}
Public class class2
{
Class1 c1obj = new class1();
Public void method2()
{
Console.WriteLine("Method2 value is " +a);
}
}
Class program
{
Static void main()
{
Class1 c1obj1 = new class1();
Console.WriteLine("a value is " +c1obj1.a);
c1obj1.method1();
Class2 c2obj1 = new class2();
c2obj1.method2();
Console.ReadLine();
}
}
}
Output: a value is 25
Method1 value is 25
Method2 value is 25
Protected Internal: If we declare a class or class member access modifier as a protected internal which can be accessed by all the classes of current project as well as derived classes from other projects within that application.
Constructors & Destructors:
Constructor: Constructor is a member of a class it will invoke automatically when an object is creative.
Constructor name same as class name with in the constructor we will write the initialization code.
Constructor will not return any value due to that reason it will not have the return types.
Syntax to define Constructor:
<access modifier> <classname()>
{
// Initialization code
}