There are modifiers or specifiers used to define scope of a type or it's members.i.e who can access them and who can't access them.
C#.Net has five access specifiers . Those are
C#.Net has five access specifiers . Those are
- private
- internal
- protected
- protected internal
- public
Members declared as private under a class were not accessible Outside of the class. Default scope for members of a class is private only.
A class under a namespace can't be declare as private.
Protected :
Members declared as protected under a class can be accessed only from child class. A non-child class can't consume them.
A class under a namespace can't be declared as protected also.
Internal :
Members declared as internal under the class for accessible only with in the project either from a child class or non-child class is internal only .
Protected Internal :
Members declared as protected internal under a class enjoy dual scope i.e with in the project the behave has internal members as project to access classes. Outside the project the change to project and still provides access to that child class.
Public :
A class or members of a class if declared as public where global can be accessed for anywhere.
Examples1 :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
public class accessspecifiers
{
private void test1()
{
Console.WriteLine("private method");
}
internal void test2()
{
Console.WriteLine("internal method");
}
protected void test3()
{
Console.WriteLine("protected method");
}
protected internal void test4()
{
Console.WriteLine("protected internal method");
}
public void test5()
{
Console.WriteLine("public method");
}
static void Main()
{
accessspecifiers c = new accessspecifiers();
c.test1();
c.test2();
c.test3();
c.test4();
c.test5();
Console.ReadLine();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class access : accessspecifiers
{
static void Main()
{
access a = new access();
// a.test1(); Note : private members can't be inherited.
a.test2();
a.test3();
a.test4();
a.test5();
Console.ReadLine();
}
}
}
Output:
internal method
protected method
protected internal method
public method
Examples1 :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
public class accessspecifiers
{
private void test1()
{
Console.WriteLine("private method");
}
internal void test2()
{
Console.WriteLine("internal method");
}
protected void test3()
{
Console.WriteLine("protected method");
}
protected internal void test4()
{
Console.WriteLine("protected internal method");
}
public void test5()
{
Console.WriteLine("public method");
}
static void Main()
{
accessspecifiers c = new accessspecifiers();
c.test1();
c.test2();
c.test3();
c.test4();
c.test5();
Console.ReadLine();
}
}
}
Output :
private method
internal method
protected method
protexted internal method
public method
private method
internal method
protected method
protexted internal method
public method
example2 :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class access : accessspecifiers
{
static void Main()
{
access a = new access();
// a.test1(); Note : private members can't be inherited.
a.test2();
a.test3();
a.test4();
a.test5();
Console.ReadLine();
}
}
}
Output:
internal method
protected method
protected internal method
public method
example3 :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class access1 : accessspecifiers
{
static void Main()
{
accessspecifiers p = new accessspecifiers();
p.test2();
p.test4();
p.test5();
Console.ReadLine();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace accessdemo
{
class access3 : ConsoleApplication1.accessspecifiers
{
static void Main()
{
access3 ac = new access3();
ac.test3();
ac.test4();
ac.test5();
Console.ReadLine();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace accessdemo
{
class access4
{
static void Main()
{
ConsoleApplication1.accessspecifiers ca = new ConsoleApplication1.accessspecifiers();
ca.test5();
Console.ReadLine();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class access1 : accessspecifiers
{
static void Main()
{
accessspecifiers p = new accessspecifiers();
p.test2();
p.test4();
p.test5();
Console.ReadLine();
}
}
}
Output :
internal method
protected internal method
public method
internal method
protected internal method
public method
Example 4 :
Open the solution explorer right click on the solution and select add new project. In the new project window choice Console Application Template and name it as accessdemo. add the class as name it as access3.cs of a new project .The Solution Explorer. Now Right click on the accessdemo to project in solution explorer and select and reference . Click browse and choice the Assemebly of ConsoleApplication1 project from it's physical location.
under class access3.cs write the following code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace accessdemo
{
class access3 : ConsoleApplication1.accessspecifiers
{
static void Main()
{
access3 ac = new access3();
ac.test3();
ac.test4();
ac.test5();
Console.ReadLine();
}
}
}
OutPut :
protected method
protected internal method
public method
Example 5:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace accessdemo
{
class access4
{
static void Main()
{
ConsoleApplication1.accessspecifiers ca = new ConsoleApplication1.accessspecifiers();
ca.test5();
Console.ReadLine();
}
}
}
Output :
public method
public method
No comments:
Post a Comment