当前位置:网站首页>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;}}}
}
边栏推荐
- dedecms织梦tag标签不支持大写字母修复
- Please write the SparkSQL statement
- What is the function of industrial-grade remote wireless transmission device?
- Flutter Learning 4 - Basic UI Components
- 延迟加载js方式async与defer区别
- Flutter学习三-Flutter基本结构和原理
- 【学生毕业设计】基于web学生信息管理系统网站的设计与实现(13个页面)
- Is the NPDP certificate high in gold content?Compared to PMP?
- Detailed explanation of Mysql's undo log
- Machine Learning Overview
猜你喜欢
The production method of the powered small sailboat is simple, the production method of the electric small sailboat
AUTOCAD——标注关联
Mysql的undo log详解
A 35-year-old software testing engineer with a monthly salary of less than 2W, resigns and is afraid of not finding a job, what should he do?
bytebuffer internal structure
JeeSite New Report
服务器磁盘阵列
Analyses the mainstream across technology solutions
[BSidesCF 2019] Kookie
Day019 方法重写与相关类的介绍
随机推荐
What is ASEMI photovoltaic diode, the role of photovoltaic diode
dedecms报错The each() function is deprecated
Flutter学习4-基本UI组件
Detailed explanation of Mysql's undo log
u-boot debugging and positioning means
ESP32 485光照度
[8.1] Code Source - [The Second Largest Number Sum] [Stone Game III] [Balanced Binary Tree]
In the WebView page of the UI automation test App, the processing method when the search bar has no search button
creo怎么测量点到面的距离
1007 Climb Stairs (贪心 | C思维)
什么是ASEMI光伏二极管,光伏二极管的作用
Talk about 20 common problems in data governance
About the installation of sklearn library
Feature preprocessing
University Physics---Particle Kinematics
1007 Climb Stairs (greedy | C thinking)
社区分享|腾讯海外游戏基于JumpServer构建游戏安全运营能力
机器学习概述
MySQL中控制导出文件后变为了\N有什么解决方案吗?
dedecms error The each() function is deprecated