Sie sind auf Seite 1von 17

ACCESS SPECIFIERS (CSHARP)

PUBLIC ACCESS SPECIFIERS (C#)

In this chapter you will learn:

What is public access specifier?

What is the boundary of public access specifier?

How to use public access specifier in C# programming?


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:

1
2
3
4
5
6
7
8
9
1
0
1
1
1
2
1
3
1
4
1
5
1
6
1
7

using
using
using
using

System;
System.Collections.Generic;
System.Linq;
System.Text;

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

1
8
1
9
2
0
2
1
2
2
2
3
ac.name = Console.ReadLine();
2
ac.print();
4
2
Console.ReadLine();
5
}
2
}
6
}
2
7
2
8
2
9
3
0
3
1
3
2

Output
Enter your name:

Steven Clark

My name is Steven Clark


__

Summary
In this chapter you learned about what public access specifier is and what
the boundary of public access specifier is. You also learned how to use public
access specifier in C# programming. In next chapter you will learn
about Private Access Specifier in C#.

PRIVATE ACCESS SPECIFIERS (C#)

In this chapter you will learn:

What is Private Access Specifier?

What is the boundary of private access specifier in C#?

How to use private access specifier in C# programming?


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:

1
2
3
4
5
6
7
8
9
1
0
1
1
1
2
1
3
1
4
1
5
1
6
1
7
1
8
1
9

using
using
using
using

System;
System.Collections.Generic;
System.Linq;
System.Text;

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();
}
}

2
0
2
1
2
2
2
3
2
4
2
}
5
2
6
2
7
2
8
2
9
3
0

Output
Error 1: Private_Access_Specifiers.access.name is inaccessible due to its protection level __

In the above example, you cannot call name variable outside the class because
it is declared as private.

Summary
In this chapter you learned what is the private access specifier in C#? You
also learned its scope and boundary and implementation in programming. In
next chapter you will learn Protected Access Specifier in C#.

PROTECTED ACCESS SPECIFIERS C#

In this chapter you will learn:

What is protected access specifier?

What is the boundary of protected access specifier?

How to use protected access specifier in C# programming?


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:

1
2
3
4
5
6
7
8
9
1
0
1
1
1
2
1
3

using
using
using
using

System;
System.Collections.Generic;
System.Linq;
System.Text;

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

1
4
1
5
1
6
1
7
1
8
1
class Program
9
{
2
static void Main(string[] args)
0
{
2
access ac = new access();
1
Console.Write("Enter your name:\t");
2
// raise error because of its protection level
2
ac.name = Console.ReadLine();
2
ac.print();
3
Console.ReadLine();
2
}
4
}
2
}
5
2
6
2
7
2
8
2
9
3
0

Output
Protected_Specifier.access.name is inaccessible due to its protection level.
This is because; the protected member can only be accessed within its child class. You can
use protected access specifiers as follow: __

Example:

1
2
3
4
5
6
7
8
9
1
0
1
1
1
2
1
3
1
4
1
5
1
6
1
7
1
8
1
9
2
0
2
1
2
2
2
3
2
4
2
5
2
6
2
7
2
8
2
9

using
using
using
using

System;
System.Collections.Generic;
System.Linq;
System.Text;

namespace Protected_Specifier
{
class access
{
// String Variable declared as protected
protected string name;
public void print()
{
Console.WriteLine("\nMy name is " + name);
}
}
class Program : access // Inherit access class
{
static void Main(string[] args)
{
Program p = new Program();
Console.Write("Enter your name:\t");
p.name = Console.ReadLine(); // No Error!!
p.print();
Console.ReadLine();
}
}
}

Output
Enter your name:

Steven Clark

My name is Steven Clark


__

Summary
In this chapter you learned about protected access specifier. You also
learned its boundary and scope and implementation in a program. In next
chapter you will learn about internal access specifier in C#.

C# INTERNAL ACCESS SPECIFIERS

In this chapter you will learn:

What is internal access specifier?

What is the boundary of internal access specifier?

How to use internal access specifier in C# programming?


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:

1
2
3
4
5
6
7
8
9
1
0
1
1
1
2

using
using
using
using

System;
System.Collections.Generic;
System.Linq;
System.Text;

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

1
3
1
4
1
5
1
6
1
7
1
8
1
9
2
0
2
1
2
2
2
3
2
4
2
}
5
2
6
2
7
2
8
2
9
3
0

}
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();
}
}

Output
Enter your name:

Steven Clark

My name is Steven Clark


__

Summary
In this chapter you learned about internal access specifier, its boundary and
scope and implementation in program. In next chapter you will
learn protected internal access specifier in C#.

C# PROTECTED INTERNAL ACCESS SPECIFIERS

In this chapter you will learn:

What is protected internal access specifier?

What is the boundary of protected internal access specifier?

How to use protected internal access specifier in C# programming?


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:

1
2
3
4
5
6
7
8
9
1
0
1
1
1
2
1
3
1
4
1
5
1
6
1
7
1
8
1
9
2
0

using
using
using
using

System;
System.Collections.Generic;
System.Linq;
System.Text;

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();
}
}
}

2
1
2
2
2
3
2
4
2
5
2
6
2
7
2
8
2
9
3
0
3
1

Output
Enter your name:

Steven Clark

My name is Steven Clark


__

Summary
In this chapter you learned about protected internal access specifier in C#.
In next chapter you will learn get set modifier in C#.

C# GET SET MODIFIER

In this chapter you will learn:


What is get set modifier?
What is the benefit of using get set modifier?


How to use get set modifier in C# programming?
The get set accessor or modifier mostly used for storing and retrieving value
from the private field. The get accessor must return a value of property type
where set accessor returns void. The set accessor uses an implicit parameter
called value. In simple word, the get method used for retrieving value from
private field whereas set method used for storing value in private variables.

Example:

1
2
3
4
5
6
7
8
9
1
0
1
1
1
2
1
3
1
4
1
5
1
6
1
7
1
8
1
9
2
0
2
1
2
2
2
3
2
4
2
5
2
6
2
7
2

using
using
using
using

System;
System.Collections.Generic;
System.Linq;
System.Text;

namespace Get_Set
{
class access
{
// String Variable declared as private
private static string name;
public void print()
{
Console.WriteLine("\nMy name is " + name);
}
public string Name //Creating Name property
{
get //get method for returning value
{
return name;
}
set // set method for storing value in name field.
{
name = value;
}
}
}
class Program
{
static void Main(string[] args)
{
access ac = new access();
Console.Write("Enter your name:\t");
// Accepting value via Name property
ac.Name = Console.ReadLine();
ac.print();
Console.ReadLine();
}
}
}

8
2
9
3
0
3
1
3
2
3
3
3
4
3
5
3
6
3
7
3
8
3
9
4
0
4
1
4
2

Output
Enter your name: Steven Clark
My name is Steven Clark
_

Summary

In this chapter you learned what get set modifier is and how to use it in
program. In next chapter, some programming examples are given. It will help
you to understand the programming concepts more clearly.

Das könnte Ihnen auch gefallen