如何在 C# 中创建只读对象属性?
如下所示,用户可以更改只读产品字段/属性:
class Program
{
static void Main(string[] args)
{
var product = Product.Create("Orange");
var order = Order.Create(product);
order.Product.Name = "Banana"; // Main method shouldn't be able to change any property of product!
}
}
public class Order
{
public Order(Product product)
{
this.Product = product;
}
public readonly Product Product;
public static Order Create(Product product)
{
return new Order (product);
}
}
public class Product
{
private Product(){}
public string Name { get; set; }
public static Product Create(string name)
{
return new Product { Name = name };
}
}
我认为这很基本,但似乎并非如此。
如何在 C# 中创建只读对象属性或字段?!
谢谢,
As can be seen below, the user is able to change the readonly product field/property:
class Program
{
static void Main(string[] args)
{
var product = Product.Create("Orange");
var order = Order.Create(product);
order.Product.Name = "Banana"; // Main method shouldn't be able to change any property of product!
}
}
public class Order
{
public Order(Product product)
{
this.Product = product;
}
public readonly Product Product;
public static Order Create(Product product)
{
return new Order (product);
}
}
public class Product
{
private Product(){}
public string Name { get; set; }
public static Product Create(string name)
{
return new Product { Name = name };
}
}
I thought it's quite basic but it doesn't seem so.
How to Create a Read-Only Object Property or Field in C#?!
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
readonly
关键字阻止您将新实例放入该字段。它不会神奇地使字段内的任何对象变得不可变。
如果你
想要一个不可变的对象,你需要通过删除所有公共 setter 来使类本身不可变。
The
readonly
keyword prevents you from putting a new instance into the field.It doesn't magically make any object inside the field immutable.
What do you expect to happen if you write
If you want an immutable object, you need to make the class itself immutable by removing all public setters.
您需要将 Product 的 Name-property 设置为私有:
You need to make the Name-property of Product private set:
您看到的问题是您将
readonly
修饰符与您认为的只读属性混淆了。readonly
修饰符确保该字段只能通过初始化或构造函数分配,例如,这里是readonly
的有效用法:您发现的是,您的
>Person
类本身是可变的,这意味着虽然字段Order.Product
是只读的,但Person
的内部结构却不是。为此,如果您想创建只读属性,您可能希望将类型创建为不可变的 - 因为其内部结构/值无法更改。The problem you are seeing is that you're confusing the
readonly
modifier with what you think is a read-only property. Thereadonly
modifier ensures that the field can only be assigned to through initialisation or a constructor, e.g here are valid uses ofreadonly
:What you are finding, is that your
Person
class itself is mutable, this means although the fieldOrder.Product
is readonly, the internal structure ofPerson
is not. To this end, if you want to make a readonly property, you'll likely want to create your type as immutable - being that its internal structure/values cannot change.只读字段总是可以在构造函数中修改(正如您所做的那样)。如果您尝试在其他地方编辑该字段,您应该会收到编译器错误。
Readonly fields can always be modified in the constructor (as you're doing). If you try to edit the field elsewhere, you should get a compiler error.
readonly 关键字意味着该字段只能在构造函数中设置该值无法更改。
如果值是引用类型,则不会使对象变为只读。
如果要将属性设为只读,只需省略 setter 即可。
the readonly keyword means the field can only be set in the constructor & the value cannot be changed.
If the value is a reference type, it doesn't make the object read-only
If you want to make the property readonly, just omit the setter.
为了更好地演示
readonly
的属性:将属性Name的setter设置为私有后(例如
public string Name { get; private set; }
):此解决方案的问题是当然是:
你有两个选择:
public string Name { get; }
,使 Product 实现此接口,然后将 Order 类型的 Product 字段定义为:public readonly IProduct Product;
此解决方案都将实现您的两个要求:
任何 他们:
To better demonstrate the properties of
readonly
:After making setter of property Name private (e.g.
public string Name { get; private set; }
):The problem with this solution, of course is:
You have 2 options:
public string Name { get; }
, make Product implement this interface, and then make Product field of type Order defined as:public readonly IProduct Product;
Any of this solutions will achieve both of your requirements:
With only difference between them: