Tag: getter setter

为什么集合初始化程序与仅使用getter的属性一起工作?

这对我来说是非常不可预测的代码结果。 我没想到这段代码会产生这样的结果。 所以,我读了Jeffrey Richter的书(clr ia c#),这个代码有一个例子。 internal class ClassRoom { private List _students = new List(); public List Students { get { return _students; } } } ClassRoom classRoom = new ClassRoom { Students = {“Mike”, “Johny”, “Vlad”, “Stas”} }; foreach (var some in classRoom.Students) { Console.WriteLine(some); } 并且,正如你们中的一些人可以建议我们可以在控制台中看到四个名字。 所以,我的问题是: 我们如何得到我们的名字,如果他们要进入学生和分别在_学生中,因为学生不存在而没有被分配。 __________EDIT_1_______________ 我读了你的答案,谢谢你们。 但是,你知道,这是一种误解……我真的无法弄明白。 当我初始化学生时 […]

在Visual Studio中生成Getter和Setter的简便方法

有没有办法在Visual Studio中生成getter和setter? 我正在尝试用Alt + R , F和我得到这个: public String Denomination { get { return denomination; } set { denomination = value; } } 我想要的是这个: public String getDenomination() { return Denomination; } public void setDenomination(String Denomination) { this.Denomination = Denomination; } 有没有办法做到这一点?

C#getter和setter简写

如果我对这条线的内部运作的理解是正确的: public int MyInt { get; set; } 然后在幕后做这个: private int _MyInt { get; set; } Public int MyInt { get{return _MyInt;} set{_MyInt = value;} } 我真正需要的是: private bool IsDirty { get; set; } private int _MyInt { get; set; } Public int MyInt { get{return _MyInt;} set{_MyInt = value; IsDirty = true;} } 但我想写一些类似于: […]

发生了’System.StackOverflowException’类型的未处理exception

为什么这个? 这是我的代码: public class KPage { public KPage() { this.Titolo = “example”; } public string Titolo { get { return Titolo; } set { Titolo = value; } } } 我通过构造函数设置数据。 所以,我想做些喜欢的事情 KPage page = new KPage(); Response.Write(page.Titolo); 但是我得到了这个错误: set { Titolo = value; }

如何为字典编写getter和setter?

如何为复杂数据类型(如字典)定义getter和setter? public Dictionary Users { get { return m_Users; } set { m_Users = value; } } 这会返回整个字典吗? 你能编写setter来查看是否存在特定的键值对,然后如果不存在,则添加它。 否则更新当前的键值对? 对于get,您可以返回特定的键值对而不是整个字典吗?

将setter添加到派生接口

是否有可能以某种方式在C#中实现此行为: public interface IReadOnly { Data Value { get; } } internal interface IWritable : IReadOnly { Data Value { get; set; } } 我希望能够向外部程序集公开一个只读接口,但在内部使用可写接口(我也可以用不同的方式实现)。 我知道我可以使用一个实现IReadOnly的抽象类但添加了setter,但这迫使我从该类派生所有内部实现。

将CSV数据导入C#类

我知道如何阅读和显示.csv文件的一行。 现在我想解析该文件,将其内容存储在数组中,并将这些数组用作我创建的某些类的值。 我想学习如何。 这是一个例子: basketball,2011/01/28,Rockets,Blazers,98,99 baseball,2011/08/22,Yankees,Redsox,4,3 如您所见,每个字段用逗号分隔。 我创建了Basketball.cs和Baseball类,它是Sport.cs类的扩展,它包含以下字段: private string sport; private string date; private string team1; private string team2; private string score; 我理解这是简单的,并且有更好的方法来存储这些信息,即为每个团队创建类,使日期成为DateType数据类型,以及更多相同但我想知道如何将此信息输入到类。 我假设这与getter和setter有关…我也读过字典和集合,但我想通过将它们全部存储在数组中来开始简单…(如果这有意义.. 。随意纠正我)。 这是我到目前为止所拥有的。 它所做的就是阅读csv并在控制台上鹦鹉学舌: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; namespace Assign01 { class Program { static void Main(string[] args) { string line; FileStream aFile = new […]

C#getters,setters声明

可能重复: 为什么要使用getter和setter? C#3.0自动属性 ​​- 有用与否? 以下方式定义属性之间是否存在差异 – // private, with getter & setter private string fName; public string Name { get { return this.fName } set { this.fName = value } } // define as a Property public string Name { get; set;} 据我所知,它看起来只是一种风格偏好。 我错过了什么吗?