Home  >  Article  >  Backend Development  >  Private in C#

Private in C#

WBOY
WBOYOriginal
2024-09-03 15:29:03768browse

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.

Private in C#

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
}

How Private Keyword Works in C# as Access Modifier?

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:

  • Private, Public, Protected access modifier can be a class member function.
  • Public and protected access modifiers can be used as derived classes.
  • A public access modifier can be only out of a class, and it cannot be derived.

How to Implement Private in C#?

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:

Private in C#

Implement Private in C#

  • As we discussed earlier in the definition and syntax part, here in implementation, we can draw our idea more clear, how they are used and called by the class function.
  • Access specifiers Private, Public, protected are also called labels or visibility labels. They were declared private can be accessed only within the class, and data in C# programming are hidden using private.
  • The programming language which goes from class and declaration public and protected are less restrictive to the access in class or from the other class, But there are programs that have written and read data privately in order to hide the data and classify it to calculate or read for another algorithm that is used by the public, Where the program declares private than public or protected is more restrictive to access data in class or member it’s declared or by other class similarly.
  • By default, member function or variable is private if none of the modifiers or specifier is mentioned. In the case of accessing a private member, there will be a compile error. Let’s look at the short program with data, where the syntax is recalled.

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.

Advantages of Using Private in C#

Below are the points that explain the advantages of using Private in C#:

  • This access specifier used to make and hide the data
  • Where the private modifier concept is more advanced and secure compared to public and protected access modifiers.
  • They can be called from the friend class function in the exceptional cases and interrelated to the continuous of the algorithm or data used in programming.
  • It’s a unique modifier to set the data, and new values to the algorithm compared to using public or protect access modifiers under-declaring into the class.
  • Private modifiers using a list and getting values through void function can be called without any parameters to the function.
  • Private fields are the primary key where it is easy to test and maintain due to its default accessibility.

Rules and Regulations for Private in C#

Following are the points that explain the rules and regulation of private in C#:

  • Use Camel Case and
  • prefix with a single underscore (_) character.
  • Ex: private string _name;
  • Avoid caps and lower-case names
  • Do not declare the same type
  • Any access modifiers vary by their capitalization
  • It doesn’t start with a numeric character
  • Numeric is used suffixes to name of the identifier
  • Meaningful and specific names should be used
  • Using moodier or in a program avoid using notations – Ex: strName or iCount.
  • No use of abbreviations, use if it is known and accepted
  • Conflicting with frameworks are not accepted
  • Identifiers are used in modifiers or to the entire program but not with meaningless prefixes and suffixes.
  • Access modifiers, as to be mentioned appropriately, omitting modifiers will be unread the entire algorithm for any class or program.
  • When using private, properties should be mentioned accordingly to give public, protected or internal modifiers in the same class or friend class sometimes.
  • The Public API should support types and members of private modifiers.

Conclusion

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:C# EventHandlerNext article:C# EventHandler