In this article, we’re going to learn about Private in C#. A Keyword private is a type of access modifier used to compile-time error when accessed outside the class or member used. It is ideally used to hide the function and variables from other member classes, but the same class’s function can access the private member in it. Technically and familiarly, the private keyword is known as access specifier and modifiers where it is either attached to a property or method. So, where the private keyword is used in members to property or method, they cannot be interfered with or accessed externally to the program. Let’s see how the private keyword work and used in the # programming language.
Syntax:
Syntax or a format how particular keyword or operators must be used with constraint and note to be included in the program, and it differs from programs and programming language.
Code:
class PrivateAccess { private: //private Accsess specifier int X; //Data Member declaration void display(); // Member Function Declaration }
With a syntax and program, how it’s used in the C# program:
Code:
class circle { private: double radius; //Private Data Member public: void set Radius(double r); //Public Member Function double getDiameter(); double getArea(); double getCircumference(); }
Explanation to the above code: From the above section, the class is about the shape circle, and the program is written about the calculating of the circle radius, the diameter, the area, and the circumference. Remember that where a private keyword is used rather than a public keyword, the data member named with a private keyword is about double-radius and can be accessed only within the class radius. The public modifiers must be double-radius.
Data used in order to calculation has to be called at the output are stored and displayed under data member double radius, where the value of double radius cannot be called or accessed from other classes or members from other programs either it is accessed with the same class if the class is mentioned with private access specifier in the same program that class cannot interfere with other class in the same program, where they are cases program one or many classes.
Three different functions specify access Modifiers:
Below is the program to implement private in c#:
Code:
using System; using System.Collections; namespace ConsoleApplication1 { class Sample { private int X; public Sample() { X = 0; } public void Set(int v) { X = v; } public static Sample operator +(Sample S1, Sample S2) { Sample temp = new Sample(); temp.X = S1.X + S2.X; return temp; } public void printValue() { Console.WriteLine("Value : {0}", X); } } class Program { static void Main() { Sample S1 = new Sample(); Sample S2 = new Sample(); Sample S3 = new Sample(); S1.Set(10); S2.Set(20); S3 = S1 + S2; S1.printValue(); S2.printValue(); S3.printValue(); } } }
Output:
Code:
class PrivateAccess { private: //Private Access Specifier int X; //Data Member Declaration void display(); //Member Function Declaration }
Explanation to the above code: Above program has a class member specified with a private modifier, and data is again mentioned with keyword private, Where the private mentioned as class name is not counted as a private modifier is specified rather private: is used is an access modifier, where it is called to store and hide the data from assembly either internally or externally. The same data is displayed using the void does not take parameters in the function.
Below are the points that explain the advantages of using Private in C#:
Following are the points that explain the rules and regulation of private in C#:
The above-discussed content about private in C#, where in general private is the keyword, but how they identified as access modifiers and used accordingly, meaningful to the word and implemented in a programming language with C# standards. Thus, the importance of the access modifier private is inherited with programs to make and hide the data from other functions or classes, but to hold the parametric values on the list to member class or structure, it is used.
The above is the detailed content of Private in C#. For more information, please follow other related articles on the PHP Chinese website!