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 get method in C #

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

Share

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

This article is about how to use the get method in C #. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Properties in C # are named members of classes, interfaces, and structures. Private members can be accessed through the C # get set accessor. Such as

Public class Animal {privatestringname; public string Name

Get {return name;} set {name = value;}}

Usually the attribute name is the same as the internal member name to be accessed, but * letters should be capitalized, such as Name

Otherwise, the internal member should have a _ prefix.

The implicit parameter value in set has the type of the underlying member variable (that is, the attribute type).

Class TestAnimal {static void Main () {

Animal animal = new Animal (); animal.Name = "Lion"

/ / set System.Console.WriteLine (animal.Name)

/ / get}}

In the TestAnimal class, you can skillfully access and set the private member name. Get,set of the Animal class through the C # get,set method of Animal. If a property has only a C # get accessor, it is read-only. Write-only properties if it has only set accessors. If it has both accessors, it is a read-write property. In C # get accessor, it must be aborted in return or throw. It is a wrong programming style to change the state of object in C # get accessor. Such as

Public int Nember {return nember++;// don't do this}

But the field value is returned, or the calculated field value is returned as shown in

Public string Name {return name! = null? Name: "NCMA";} using System;public class BaseClass {private string name;public string Name {get {return name;} set {name = value;}

How to access properties in the base class that are overridden by attributes of the same name of the derived class:

Public class DerivedClass: BaseClass {

Private string name;public new string Name

/ / override the Name in the base class with the new modifier

{get

{return name;} set {name = value;}

Public class MainClass {public static void Main () {

DerivedClass D1 = new DerivedClass (); d1.Name = "John"

/ / Derived class property Console.WriteLine ("Name in the derived class is: {0}", d1.Name); ((BaseClass) D1) .Name = "Mary"

/ / Base class property Console.WriteLine ("Name in the base class is: {0}", ((BaseClass) D1) .Name)

}

C # get output:

Name in the derived class is: John

Name in the base class is: Mary

Convert the derived class object to the base class object, and then access the properties in the base class

Thank you for reading! This is the end of the article on "how to use the get method in C#". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

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