当前位置:网站首页>Understanding and use of C# on set() and get() methods
Understanding and use of C# on set() and get() methods
2022-08-05 04:53:00 【dusk and starry sky】
》set and get mean: set is to set, get is to obtain.As the name suggests, these two methods are used to set and get data.
》Closing and security in object-oriented programming in C#: Closing means enclosing the domain variables in the class, that is, decorating them with private, and other classes cannot access the variables.In this way, we enclose these variables inside the class, which improves the security of the data.
》How to operate the closed domain variable: two ways: 1. Through the public constructor, the variable is assigned as soon as the object is instantiated 2. Through the set and get methods.Generally speaking, set and get methods operate on private field variables, so most of them are used in class entities containing specific properties.
In the above example, when assigning value to the field, directly assign the value to the field
namespace code_1
{
class Book
{
private int id;
private string name;
private double price;
//Set the book number property
public int Id
{
get { return id; }
set { id = value; }
}
//Set the book name property
public string Name
{
get { return name; }
}
//Set the book price attribute
public double Price
{
get { return price; }
set { price = value; }
}
}
}
》The old version of .net framework for properties, cannot be omitted, it must be done:
int adminID = 0;
public AdminID
{
get{return adminID;}
set{adminID=value;}
}
And the current version can be omitted, just need: public AdminID{get;set;} That is, do nothing with the data.
Comparison of using get set and not using it
For example, there are two classes person:
//The first way
public class person
{
public string name;
}
//The second way
public class person
{
public string Name { set; get; }
}
1
2
3
4
5
6
7
8
9
10
11
The name attribute of the first type is not encapsulated, and its name attribute is directly exposed to other classes in the system through the public keyword
The name attribute of the second type is encapsulated by the get set keyword. The get and set are readable and writable respectively, which are equivalent to the following code:
private string name;
public string Name//This Name is preferably the same as the attribute name, (if you want to define a different name, see the bottom of the article) and the first letter must be capitalized
{
get { return name; }
set { name = value; }
}
1
2
3
4
5
6
7
When defining a class in c#, it is usually necessary to encapsulate the object declared in the class so that the outside world cannot access this property.In the above code, if the set part is removed, the outside world can only read the value of the name, and if the get part is removed, only the name can be assigned a value.In this way, you can control the access rights of the outside world to the private property name, which is a feature of C#.
Of course, you can also create your own functions to get and assign values to name, but this is more troublesome.
The difference between attributes and ordinary variables is that ordinary variables are things that are placed in the house, and what they are like is clear. And attributes are a gatekeeper placed at the door of the house, and you have to pass him when you take things and put things.
This gatekeeper is an attribute accessor, taking something is get, and putting something is set. It has the final say in how to take and put it, although you think it is almost the same as just taking it directly.
GET SET is externalInterface. Generally, accessing member data in an instance requires the access method of this member data to be PUBLIC. Now C# is very advanced, GET is to obtain, and SET is to set, but it provides good shrinkage, and GET and SET can be set separately.Access rights, such as you can only read or write to a member data, or only derived classes can access it...Compared with the past, there is no property accessor, and private member data needs to be called through functions. Properties provide an efficient access mode and simple writing.
It's for security~
The field is the specific data you want to operate, so its value can't be wrong, but you can't prevent the user from making mistakes
For security, use attributes to assign values to fields, because in setYou can verify the security of the data,
get is simpler. After verifying the data, you need to give the value to the field, so you need to get
set means "set" in Chinese;
get means in Chineseis "obtained";
Additional:
get and set will be automatically called at the corresponding time
Mainly to hide the data structure in the program, use
get to get the value of the property, and
set to set the value of the property.
There is a special variable value when using set
is the value when setting the property
value Implicit parameter used to set accessors and add or remove event handlers.
Set the properties of the class
For example, use class name.property="" to assign set function
Variable=class name.property value get function
Complete code example
If value<0, define radius=0, otherwise, assign value to radius attribute
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace test
{
class Program
{
static void Main(string[] args)
{
Circle circle = new Circle();
circle.Radius = -1;
Console.WriteLine("Radius is " + circle.Radius);//Use like this}}class Circle{const double pi = 3.1415;private double radius;public double Radius//defined like this{get => radius;set{if (value < 0) radius = 0;else radius = value;}}}}
边栏推荐
- 1068 Find More Coins
- 工业级远距离无线传输装置的功能有哪些?
- 1007 Climb Stairs (greedy | C thinking)
- 关于sklearn库的安装
- bytebuffer internal structure
- In the WebView page of the UI automation test App, the processing method when the search bar has no search button
- Four-digit display header design
- dedecms织梦tag标签不支持大写字母修复
- Machine Learning Overview
- [BJDCTF2020] EasySearch
猜你喜欢

Some conventional routines of program development (1)

Flutter学习2-dart学习

作业8.4 进程间的通信 管道与信号

bytebuffer 使用demo
![[Surveying] Quick Summary - Excerpt from Gaoshu Gang](/img/35/e5c5349b8d4ccf9203c432a9aaee7b.png)
[Surveying] Quick Summary - Excerpt from Gaoshu Gang

Dephi reverse tool Dede exports function name MAP and imports it into IDA

二叉树基本性质+oj题解析

数字孪生技术在电力系统中的应用现状

upload upload pictures to Tencent cloud, how to upload pictures

Mysql的undo log详解
随机推荐
In the WebView page of the UI automation test App, the processing method when the search bar has no search button
flink reads mongodb data source
[MRCTF2020]Ezpop(详解)
Error creating bean with name ‘configDataContextRefresher‘ defined in class path resource
There are a lot of 4T hard drives remaining, prompting "No space left on device" insufficient disk space
How to deal with DNS hijacking?
【转】什么是etcd
ansible各个模块详解
狗仔队:表面编辑多视点图像处理
淘宝账号如何快速提升到更高等级
[8.2] Code Source - [Currency System] [Coins] [New Year's Questions (Data Enhanced Edition)] [Three Stages]
重载运算符
Flutter Learning 4 - Basic UI Components
Flutter TapGestureRecognizer 如何工作
JeeSite New Report
[Nine Lectures on Backpacks - 01 Backpack Problems]
说说数据治理中常见的20个问题
[CISCN2019 华东南赛区]Web11
工业级远距离无线传输装置的功能有哪些?
Detailed explanation of each module of ansible