C# Private

  • The private keyword is one of the access modifiers with the least access level in C#.
  • The private members can only be accessed inside the class or struct which the private members are declared.
  • The private keyword supports C# Encapsulation which makes the private fields accessed by public methods without knowing the details of the fields and relationships.
Note Note
Private classes are not supported in C#

Note Note
You will get a compile time error if a private member is accessed from outside the class which declares the members.

Example 01-47-01

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using System;

namespace TestPrivate
{
    public class Student
    {
        private string name;
        private int score;

        public string getName()
        {
            return name;
        }

        public void setName(string myname)
        {
            name = myname;
        }

        public int getScore()
        {
            return score;
        }

        public bool setScore(int myscore)
        {
            if (checkScore(myscore))
            {
                score = myscore;
                return true;
            }
            else
            {
                return false;
            }
        }

        private bool checkScore(int myscore)
        {
            return myscore >= 0 && myscore <= 100;
        }
    }
    
    class TestStudent
    {
        static void Main(string[] args)
        {
            Student s = new Student();
            s.setName("John");
            if (s.setScore(90))
            {
                Console.WriteLine("Name: {0}, Score = {1}", s.getName(), s.getScore());
                //Console.WriteLine("Name: {0}, Score = {1}", s.name, s.score);
            }
            else
            {
                Console.WriteLine("Wrong Score");
            }

            s.setName("Mike");
            if (s.setScore(120))
            {
                Console.WriteLine("Name: {0}, Score = {1}", s.getName(), s.getScore());
            }
            else
            {
                Console.WriteLine("Wrong Score");
            }
            
            Console.Read();
        }
    }
}

Output

Name: John, Score = 90
Wrong Score

Explanation

  • Line 5-42: Define a public class Student.
  • Line 7-8: Declare 2 private fields of the class - name and score.
  • Line 10-13: Define a method getName() which is used to return the value of the private field "name".
  • Line 15-18: Define a method setName to set the value to the private field "name".
  • Line 20-23: The method getScore is used to get the value of the private field "score".
  • Line 25-36: The method setScore is to set the value of the parameter to the private field "score".
  • Line 27: Before setting the value, we'll check if the value is legal by calling another method "checkScore".
  • Line 38-41: This is the method "checkScore".
  • Line 40: The method return true if the score is between 0 to 100. Otherwise returns false.
  • Line 29: If checkScore returns true, the value of the parameter myscore will be assigned to the private field "score".
  • Line 30: Then returns true.
  • Line 34: Otherwise returns false.
  • Line 44-72: TestStudent class definition.
  • Line 46: Start Main method.
  • Line 48: Instantiate the class Student.
  • Line 49: Call the method setName to set "John" to the field name.
  • Line 50: If statement. The method "setScore" returns true if the score transferred is between 0 to 100
  • Line 52: Output "Name: John, Score = 90".
  • Line 53: This statement is commented out because the fields are all private and cannot be called outside class Student directly.
  • Line 60: Change the name of the object by calling the method setName.
  • Line 61: Because 120 is not between 0-100. The method setScore(120) returns false. Then go to line 67
  • Line 67: Output the "Wrong Score" message.