关键差异 - 字段VS属性在C#中
这关键区别between field and property in C# is that字段是直接在类中声明的任何类型的变量,而属性是提供灵活的机制来读取,写或计算私有字段的值的成员。
C#是Microsoft开发的现代编程语言。这是通用编程语言。通用语言接口(CLI)由运行时环境和可执行文件组成。C#是基于.NET框架的语言。它提供自动garbage collection,,,,delegates, Language Integrated Query (LINQ), etc. to write programs easily and faster. One main advantage of C# is that it supports Object Oriented Programming (OOP). It helps to construct a program or a software using objects. In a system, there is a lot of objects, and their objects pass messages using methods. Field and Property are two terms associated with OOP. This article discusses the difference between field and property in C#.
CONTENTS
1。概述和关键差异
2。C#中的字段是什么
3。C#中的属性是什么
4。C#中的字段与属性之间的关系
5.Side by Side Comparison – Field vs Property in C# in Tabular Form
6.Summary
C#中的字段是什么?
每个对象都有特征和行为。特征由田地描述,行为由方法描述。员工对象可以具有员工NO,姓名和部门等字段。
根据上面的说法,三角形是一类。它包含三个公共场,分别是Base1,Height1和区域。这constructor可以分配base1和高度的值。在主要方法中,创建了三角形的对象。它称为T1,两个值传递给基础和高度。三角形类中的构造函数将这些值分配给字段。然后,在主要方法中,调用了calarea方法。它将计算三角形的面积,并将答案分配给区域字段。最后,显示方法将调用,并将在屏幕上输出答案。
OOP的一个主要支柱是封装。它允许将字段和方法压缩到一个单元中。封装用于保护数据。访问说明符可用于更改字段和方法的可见性。可以在课堂外访问公共成员。私人成员仅在课堂内访问。为了将可访问性限制在课程中,可以将字段私有化。可以通过公共方法来完成设置和获取值。
According to the above program, the Triangle is a class. There are fields called base1 and height1. They are private fields. In the main method, an object of Triangle is created. The details method is called on the Triangle instance. The values for the base1 and height1 are accepted by the details method. Those gained values are assigned to base1 and height1 fields. In the main method, the calArea method is called on the t1 object. It calculates the area. Finally, the display method prints the area of the triangle. The fields are private, but they are accessible by the public methods.
C#中的属性是什么?
这些属性没有存储位置。这些属性具有包含可执行语句的访问者,以读取值并设置值。登录声声明可以包含GET登录器和设置访问者。假设有一个班级雇员,它包含私人领域,例如员工NO,姓名和部门。这些字段无法直接从班级外部访问。因此,程序员可以使用属性设置和获取值。因此,这些属性可用于访问私有字段。
声明类型字符串的名称属性如下。“值”关键字是指分配的值。
public string Name {
获取{返回名称;}
set {name = value;}
}
参考以下程序,
这Employee class has two private fields that are id and name. The ID and Name are properties. The id value is set and get using the property ID. The name value is set and get using the property Name. In the main method, an object of Employee is created. The private id and private name fields of Employee class are accessed using the properties. Finally, the values are displayed on the screen.
What is the Relationship Between Field and Property in C#?
- 可以使用属性访问私有字段。
C#中的字段和属性有什么区别?
Field vs Property in C# |
|
A field is a variable of any type that is declared directly in a class. | A property is a member that provides a flexible mechanism to read, write or compute the value of a private field. |
用法 | |
A field can be used to explain the characteristics of an object or a class. | 属性可用于设置和接收字段的值。 |
摘要 - 字段VS属性在C#中
在OOP中,可以使用对象对程序或软件进行建模。对象是使用类创建的。类是创建对象的蓝图。字段和属性是C#OOP中使用的两个术语。本文讨论了C#中的字段和属性之间的区别。C#中的字段和属性之间的区别在于,字段是直接在类中声明的任何类型的变量,而属性是提供灵活的机制来读取,写或计算私有字段的值。
参考:
1.“字段(C#编程指南)。”微软文档。在这里可用
2.“属性(C#编程指南)。”微软文档。在这里可用
3.“ C#属性 - Javatpoint。”,Javatpoint。在这里可用
4.“ C#类。”,教程点,,,,19 Mar. 2018.在这里可用
5.“ c#properties。”,教程点,,,,19 Mar. 2018.在这里可用
Leave a Reply