Home > Backend Development > C#.Net Tutorial > What are the default access rights for class members in C#?

What are the default access rights for class members in C#?

王林
Release: 2023-08-25 12:45:11
forward
1343 people have browsed it

C# 中类成员的默认访问权限是什么?

Default access to class members in C# is private.

Member variables (i.e. class members) are properties of the object (from a design perspective), and they are kept private to achieve encapsulation. These variables can only be accessed using public member functions.

Example

using System;

namespace RectangleApplication {
   class Rectangle {
      //member variables
      private double length;
      private double width;

      public void Acceptdetails() {
         length = 10;
         width = 14;
      }

      public double GetArea() {
         return length * width;
      }

      public void Display() {
         Console.WriteLine("Length: {0}", length);
         Console.WriteLine("Width: {0}", width);
         Console.WriteLine("Area: {0}", GetArea());
      }
   }//end class Rectangle

   class ExecuteRectangle {
      static void Main(string[] args) {
         Rectangle r = new Rectangle();
         r.Acceptdetails();
         r.Display();
         Console.ReadLine();
      }
   }
}
Copy after login

The above is the detailed content of What are the default access rights for class members in C#?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template