Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the Public access modifier of C #

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Today Xiaobian to share with you how to use the C#Public access modifier related knowledge points, detailed content, clear logic, I believe most people still know too much about this knowledge, so share this article for everyone to refer to, I hope you read this article after some harvest, let's learn about it together.

Public access modifier

The Public access modifier allows a class to expose its member variables and member functions to other functions and objects. Any public member can be accessed by external classes.

The following examples illustrate this:

using System;

namespace RectangleApplication

{

class Rectangle

{

//Member variables

public double length;

public double width;

public double GetArea()

{

return length * width;

}

public void Display()

{

Console.WriteLine("Length: {0}", length);

Console.WriteLine("Width: {0}", width);

Console.WriteLine("Area: {0}", GetArea());

}

}// Rectangle ends

class ExecuteRectangle

{

static void Main(string[] args)

{

Rectangle r = new Rectangle();

r.length = 4.5;

r.width = 3.5;

r.Display();

Console.ReadLine();

}

}

}

When the above code is compiled and executed, it produces the following results:

Length: 4.5 Width: 3.5 Area: 15.75

In the example above, the member variables length and width are declared public, so they can be accessed by the function Main() using an instance r of the Rectangle class.

The member functions Display() and GetArea() have direct access to these variables.

The member function Display() is also declared public, so it can also be accessed by Main() using an instance r of the Rectangle class.

The above is "C#Public access modifier how to use" all the content of this article, thank you for reading! I believe everyone has a great harvest after reading this article. Xiaobian will update different knowledge for everyone every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report