简体   繁体   English

C#类和只读成员

[英]C# class and readonly members

When writing a class in C#, is it a good idea to mark all of you private member variables as private readonly if they are only assigned to in the constructor and aren't subject to change elsewhere in your class? 在C#中编写一个类时,如果只将它们分配给构造函数并且不会在类的其他地方进行更改,那么将所有私有成员变量标记为私有只读是一个好主意吗? Or is this overkill? 或者这有点矫枉过正?

Yes, personally I believe it's a good idea. 是的,我个人认为这是一个好主意。 I try to keep types immutable where possible, and declaring a variable readonly is a good start to that. 我尝试尽可能保持类型不可变,并且readonly一个变量是一个很好的开始 It's not the be-all and end-all, of course - if that variable is something mutable (eg a StringBuilder or an array) then it's really not helping all that much. 当然,这并非全部和最终 - 如果该变量是可变的(例如StringBuilder或数组),那么它实际上并没有那么多帮助。 I'd still make the variable read-only though to make it obvious that I don't want to change the value of the variable itself - and to prevent myself from doing so accidentally elsewhere in the same class, possibly months or years later. 我仍然将变量设为只读,但显然我不想改变变量本身的值 - 并且防止自己在同一个类的其他地方意外地这样做,可能是几个月或几年之后。

Yes, that is what readonly specifically indicates. 是的,这是readonly具体指出的。 If you already know (or can at least assume) that you're not going to assign it anywhere else, then marking it readonly is a good idea. 如果您已经知道(或者至少可以假设)您不打算将其分配给其他任何地方,那么将其标记为readonly是一个好主意。 After all, it's easier to remove readonly than it is to add it later. 毕竟, 删除 readonly比以后添加它更容易。

Wow what a good question and one that is purely going to be answered with opinions. 哇这是一个很好的问题,而且纯粹是用意见来回答。 My opinion is I always just create properties to the variable. 我的意见是我总是只为变量创建属性。 An example is as follows. 一个例子如下。

private int _myInt;
private int myInt {get{return _myInt;}}

是的 - 您不会遇到由其他开发人员编写的某些代码修改其值的问题,这些代码不知道它们应该是只读的。

Readonly makes very much sense in situations where you pass a service reference through constructor, ie Readon在通过构造函数传递服务引用的情况下非常有意义,即

public class MyViewModel { private readonly MyContext context; public MyViewModel(MyContext context) { this.context = context; } }

You obviously don't want your context overwritten with another one, because you can have a lot of stuff dependent on that particular service inside the class. 你显然不希望你的上下文被另一个上下文覆盖,因为你可以拥有很多依赖于类中特定服务的东西。 And if it's a constructor parameter, that usually means you RELY on that particular service or object for creating and keeping the valid state of the object. 如果它是构造函数参数,通常意味着您在该特定服务或对象上使用RELY来创建和保持对象的有效状态。 So readonly is a good indicator of just that. 所以readonly就是一个很好的指标。 Having private set on property means that you can't change it outside the class, readonly is an extra constraint which makes things a bit more secure and understandable. 在属性上设置私有意味着你不能在类之外更改它,readonly是一个额外的约束,它使事情更安全和可理解。

If I'm only going to initialize a variable once and never write to it, I would make it const. 如果我只是初始化一次变量并且从不写入它,我会把它变成const。

http://en.csharp-online.net/const,_static_and_readonly http://en.csharp-online.net/const,_static_and_readonly

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM