In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
The editor will share with you the sample analysis of fields, behaviors and properties in the c # class. I hope you will gain something after reading this article. Let's discuss it together.
1. Value type and reference type of c
1. Value types are: structures (numeric types, such as int; Bool, user-defined structures), enumerated types, controllable types. The value type exists on the stack.
2. Reference types: arrays, user-defined classes, interfaces, delegates, object, strings. The reference type is stored on the heap.
(memory block is divided into stack, heap, static storage area)
3. Several points are explained:
(1) even if the structure contains members of the reference type, it is still a value type.
(2) the elements of the array, whether of reference type or value type, are stored on the managed heap.
(3) the value type is always assigned where it is declared, stored as a field with the variable (instance) to which it belongs, and on the stack as a local variable.
(4) value types are more efficient in memory management, and do not support polymorphism, so they are suitable to be used as in-memory carriers, while reference types support polymorphism and are suitable for defining the behavior of applications.
Second, the access control of class c
Public, protect, private, internal,protect internal
Public is public, any other object can be accessed, the subclass inherits, and the accessibility of the property of the other object subclass is determined by the accessibility of the subclass.
If protect is protected, subclasses can inherit, and other classes cannot access directly.
Private, which is inherited by the subclass, but is inaccessible in the subclass and cannot be accessed by other objects.
Internal, which means that it can be accessed in the same assembly
Protect internal subclasses within the same assembly can be inherited and cannot be accessed by other classes
The system is private by default.
III. Fields, methods, and properties of the class
A class is the basic unit of object-oriented programming, and its members include events, fields, nested types, methods, and properties. As beginners, we first need to understand the fields, methods, and properties in the members of the class.
1. The field of the class. The field of a class should be exactly a data member of the class, defined as a variable that stores data related to the class and its instances.
2. The method of the class. A method, also known as behavior, is a function member of a class, a function that implements a specific function of the class.
3. The properties of the class. The so-called "attribute", to a large extent, can be seen as a kind of encapsulation of "field". It uses a kind of so-called "get/set accessor" to control the read and write operation of the field and expose a property value.
Let's give an example to illustrate the role of attributes.
Class Student
{
/ / definition of data members or fields
Public string name
Private string accnumber;// here, we originally defined the student account as private.
/ / cannot be inherited and accessed.
/ / definition of attribute
Public string Accnumber
{
Get
{
Return name
}
Set
{
Accnumber = value
}
}
...
}
/ / here, using properties, the outside world can read the value of accumber through the get accessor; through the set accessor, we can assign a new value to accumber. In subclasses and other classes, we can think of Accnumber as a stand-in for accnumber, such as: Student std = std.Accnumber = "S001", and we successfully assign the value "S001" to accnumber.
Of course, not both get and set are used in the properties. When I just read the value of accnumber without changing it, I just use the get accessor. We will introduce the readonly access modifier later, which controls that variables can only be read and not written.
In the above example, get is the value and set is the setting value. In set, we can also add some restrictions and selection conditions, such as if (value.Length==4) {accnumber = value}.
A field is a variable used to store data, and a property is a method or a function member. So since an attribute is a method, what is the difference between it and a method? From the definition, we can see that the attribute itself is the method. But since properties and methods are defined as two concepts, there must be differences between them. Personally, the biggest difference between them is that the attribute does not have a parameter list, while the method must have a parameter list, even if there are no parameters, put an empty parenthesis there. Secondly, there should be set and get accessors in the attribute definition, which are used to obtain the value of the attribute and set the value of the attribute. The rest of the difference is not found, and the validity of the data can also be determined in the attribute, which is no different from the method.
After reading this article, I believe you have some understanding of "sample analysis of fields, behaviors and properties in category c #". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.