Tag: readonly

如何在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) […]

是否有可能只分配一次的字段?

我需要一个可以从我想要的地方分配的字段,但是应该可以只分配一次(因此应该忽略后续分配)。 我怎样才能做到这一点?

在C#中只读二维数组

在C#中是否有任何已建立的返回只读二维数组的方法? 我知道ReadOnlyCollection是用于1-d数组的正确选择,我很乐意编写自己的包装类来实现this[] {get} 。 但是如果这个轮子已经存在,我不想重新发明轮子。

在仅由构造函数调用的私有方法中赋值readonly变量

C#编译器给了我以下错误 CS0191:无法分配只读字段(构造函数或变量初始化程序除外) 我是否必须将代码(在我的私有函数中)移动到构造函数中? 这听起来很尴尬。 请注意,私有方法仅用于构造函数调用。 我希望我可以使用某种属性来标记相应的方法。

修改私有readonly成员变量?

我有以下代码: public class MyClass { private readonly string name; public string Name { get { return name; } } public MyClass(string name) { this.name = name; } } class Program { static void Main(string[] args) { MyClass mc = new MyClass(“SomeName”); } } 有没有什么办法可以在不修改类的情况下更改mc.name的值?

具有丰富类型的C#中的const正确性

来自C ++背景并尝试学习C#,我遇到的最令人沮丧的语言遗漏之一是与const关键字等效。 所以,我一直试图找到一种模式,我可以用它来实现C#中的const正确性。 这个答案有一个有趣的建议:为你的所有类型创建一个只读界面。 但正如Matt Cruikshank在评论中指出的那样,如果你的class级有collections品或其他丰富的类型,这就成了问题。 特别是如果您无法控制类型,并且无法使其实现只读接口。 是否存在可以处理丰富类型和集合的任何模式或解决方案,或者我们是否强制在C#中进行复制? 在C#中完全放弃const正确性是否更好?

C#:为什么readonly结构上的突变没有破坏?

在C#中,如果你有这样的struct : struct Counter { private int _count; public int Value { get { return _count; } } public int Increment() { return ++_count; } } 你有一个这样的程序: static readonly Counter counter = new Counter(); static void Main() { // print the new value from the increment function Console.WriteLine(counter.Increment()); // print off the value stored in […]

我在Visual Studio中的项目是只读的。 我做了什么?

我一定做错了。 我在Visual Studio 2008中有一个C#项目。突然间,我看到了对我的类的锁定,当我将类名称hover在顶部选项卡上时,我看到类名称为:C:\ Myprojects \ Oder.cs [只读] ]! 有没有像你之前发生的奇怪事情?

以编程方式更改DataGridView行上的ReadOnly模式

没有解释整个上下文,我的问题基本上是这样的: 我在Windows窗体上有一个datagridview,它绑定到Entity Framework DbSet: dbSet.Local.ToBindingList() 。 如果我将datagridview的ReadOnly属性设置为true(在设计视图中),然后在我的代码中包含此语句: myDataGridView.Rows[rowIndex].ReadOnly = false; 它直接通过而不改变价值! (不,我的数据源不是只读的。) 循环遍历行中的单元格并单独设置每个单元格的ReadOnly属性也不起作用: foreach (DataGridViewCell cell in myDataGridView.Rows[rowIndex].Cells) { cell.ReadOnly = false; } .ReadOnly – Gets or sets a value indicating whether the user can edit the cells of the DataGridView control. 但它表现得像某种原因无法设定 。 这有可能发生吗? 我在想,也许datagridview的ReadOnly属性会覆盖对特定行/单元格所做的任何更改(即整个表单必须是readonly或不是),但显然这适用于某人…… 还有其他方法可以实现我的最终目标,但我想知道为什么我不能改变这个属性,因为这对我的情况来说是最简单的解决方案。 编辑: 让我试着澄清一下我的问题: 同样,我知道还有其他方法可以实现我的最终目标,但是想要回答这个问题: 抱歉原始示例,但要轻松复制问题,创建一个WinForm应用程序,在其上抛出一个datagridview,并将此代码传递到您的项目中: public partial class Form1 […]

覆盖子类中的ReadOnly属性以使其成为读/写(VB.NET或C#)

这在具有属性的VB.NET中似乎不可能,因为属性语句本身必须描述它是否是ReadOnly 。 在我下面的例子中,它不允许我编译ReadWriteChild 。 我想我可以让父读/写,然后让ReadOnlyChild的setter不做任何事情,但这似乎有点hacky。 在这种情况下,最好的替代方案似乎是放弃属性而采用getter / setter方法。 Public MustInherit Class Parent Public MustOverride ReadOnly Property Foo() As String End Class Public Class ReadOnlyChild Inherits Parent Public Overrides ReadOnly Property Foo() As String Get ‘ Get the Property End Get End Property End Class Public Class ReadWriteChild Inherits Parent Public Overrides Property Foo() As String […]