Skip to main content

Private Access Specifiers (C#)


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:

using System;

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



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.

 


Comments

Popular posts from this blog

Write a program to enter a number from 1 to 7 and display the corresponding day of the week. Hint: 1 = Monday ..

using System; class sevenday { public static void Main() { int num1; Console.WriteLine("Enter Number From 1 - 7 to find the correponding day"); num1=Convert.ToInt32(Console.ReadLine()); switch(num1) { case 1: Console.WriteLine(" 1 is Monday"); break; case 2: Console.WriteLine("2 is tuesday"); break; case 3: Console.WriteLine("3 is wednesday"); break; case 4: Console.WriteLine("4 is Thrusday"); break; case 5: Console.WriteLine("5 is friday"); break; case 6: Console.WriteLine("6 is saturday"); break; case 7: Console.WriteLine("7 is sunday"); break; default: Console.WriteLine(" Your Number is Invalid Please Enter Correct Number"); break; } } }

Working with Parameter in C#

Parameter is used to passing information to a function. Based on type of parameters, a function responses and returns value. A parameter can be passed to a function by two ways: Passing parameter by value and passing parameter by reference. 

C# Loop Examples

In this chapter you will learn how to implement loop constructs in C# programming . There are some programming examples are given below that will help you to understand loop constructs in C#. Qu 1: Write a program to display table of given number. Example: using System; namespace Examples1 {    class Program    {      static void Main( string [] args)        {          int num, i,result;          Console .Write( "Enter a number\t" );          num = Convert .ToInt32( Console .ReadLine());          for (i = 1; i <= 10; i++)          {            result = num * i;            Console .WriteLine( "{0} x {1} = {2}" , num, i,                result);        ...