C# 中的字段与属性
本教程将解释 C# 类中字段和属性之间的区别。为了更好地说明这一点,你必须首先了解以下概念。
什么是类
类是数据成员的集合,使你能够通过充当对象的蓝图或模板来创建自定义类型。在一个类中,字段和属性的相似之处在于它们可以存储数据,但不同之处在于它们的使用方式。
什么是字段
field
将数据保存为变量。它可以公开,然后通过类访问,但最好的做法是限制和隐藏尽可能多的数据。通过确保人们可以访问数据的唯一方法是利用类的公共方法之一,这为你提供了更多控制并避免了意外错误。
什么是属性
属性公开字段。使用属性而不是字段直接提供了一个抽象级别,你可以在其中更改字段,同时不影响使用你的类的对象访问它们的外部方式。属性还允许你在设置字段值或确保数据有效之前进行计算。
例子:
using System;
namespace FieldProperty_Sample {
class Program {
static void Main(string[] args) {
// Initialize a new Tutorial_Class
Tutorial_Class tutorial = new Tutorial_Class();
// Set the field values through its public property
tutorial.Name = "Sample Tutorial";
tutorial.Created_Date = new DateTime(2021, 12, 31);
tutorial.Description = "Sample Description";
// Print the class values to the console
Console.WriteLine("Tutorial Name: " + tutorial.Name);
Console.WriteLine("Created Date: " + tutorial.Created_Date.ToShortDateString());
Console.WriteLine("Sample Description: " + tutorial.Description);
Console.ReadLine();
}
public class Tutorial_Class {
// These are the private fields that are only accessible by the class
// The actual data is stored here and can be retrieved/set by the properties
private string name;
private DateTime created_date;
// All of the items below are properties that have the power to get and set the field values
public string Name {
get { return name; }
set { name = value; }
}
public DateTime Created_Date {
get { return created_date; }
set { created_date = value; }
}
// This is another kind of property, an AutoProperty
// It acts the same as the original property but does not require you to declare the private
// field The private field is automatically generated for you
public string Description { get; set; }
}
}
}
在上面的示例中,我们有一个使用字段、属性和 AutoProperties 的示例类。初始化 Tutorial_Class 的实例后,我们使用类的公共属性设置值。你可以注意到,如果你尝试直接访问这些字段,你将收到错误 Program.Tutorial_Class.x is inaccessible due to its protection level
。最后,我们将值打印到控制台以显示已进行更改。
输出:
Tutorial Name: Sample Tutorial
Created Date: 31/12/2021
Sample Description: Sample Description
相关文章
在 C# 中将 List<string>转换为字符串
发布时间:2024/03/16 浏览次数:198 分类:编程语言
-
在 C# 中,有两种主要方法可用于将 List
转换为字符串变量,Linq 方法和 String.Join()函数。
在 C# 中将 List<string>转换为字符串
发布时间:2024/03/16 浏览次数:171 分类:编程语言
-
在 C# 中,有两种主要方法可用于将 List
转换为字符串变量,Linq 方法和 String.Join()函数。
在 C# 中将 List<string>转换为字符串
发布时间:2024/03/16 浏览次数:187 分类:编程语言
-
在 C# 中,有两种主要方法可用于将 List
转换为字符串变量,Linq 方法和 String.Join()函数。
在 C# 中发出 HTTP POST Web 请求
发布时间:2024/02/04 浏览次数:131 分类:编程语言
-
在 C# 中,可以使用 3 种主要方法来发出 HTTP POST Web 请求:WebClient 类,HttpWebRequest 类和 HttpClient 类。本教程将讨论在 C# 中发出 HTTP POST Web 请求的方法。使用 C# 中的 WebClient 类发出 HTTP POST Web 请求
在 C# 中运行命令提示符命令
发布时间:2024/02/04 浏览次数:130 分类:编程语言
-
Process 类可用于在 C# 中运行命令提示符命令。在 C# 中使用 Process.Start() 函数运行命令提示符命令
在 C# 中调整图像大小
发布时间:2024/02/04 浏览次数:203 分类:编程语言
-
有两种主要方法可用于在 C# 中调整图像的大小,Bitmap 类构造函数和 graphics.DrawImage()函数。在本教程中,我们将讨论在C#中调整图像大小的方法。我们将带您完成整个过程,从加载原始图像到保
在 C# 中下载图片
发布时间:2024/02/04 浏览次数:138 分类:编程语言
-
有 3 种主要方法可用于下载 C# 中的图片,WebClient.DownloadFile()函数,Bitmap 类和 Image.FromStream()函数。在 C# 中使用 WebClient 类下载图片 WebClient 类提供了用于向 C# 中的 URL 发送数据和从 URL 接收数据
在 C# 中使用秒表
发布时间:2024/02/04 浏览次数:139 分类:编程语言
-
我们可以使用 Stopwatch 类来计算 C# 中的经过时间。使用 C# 中的秒表类计算经过时间 Stopwatch 类在 C# 中准确测量经过的时间。
在 C# 中获取可执行路径
发布时间:2024/02/04 浏览次数:200 分类:编程语言
-
有 3 种主要方法可用于获取 C# 中程序的可执行路径,即 Assembly 类,AppDomain 类和 Path 类。本教程将介绍获取 C# 代码的可执行路径的方法。使用 C# 中的 Assembly 类获取可执行路径