Beruflich Dokumente
Kultur Dokumente
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
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#.
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#.
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
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#.
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
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#.
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
Summary
In this chapter you learned about protected internal access specifier in C#.
In next chapter you will learn get set modifier in C#.
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.