Tag: class

在嵌套属性上使用XmlAttributeOverrides

我正在尝试使用XmlAttributeOverrides来控制在序列化类之后哪些类属性出现在xml中。 它适用于“根”类上的属性,但不适用于嵌套属性。 这是一个简单的例子来说明我想要完成的事情。 我的类层次结构如下: public class Main { public string Name { get; set; } public Location Address { get; set; } } public class Location { public string StreetAddress { get; set; } public Contact ContactInfo{ get; set; } } public class Contact { public string PhoneNumber { get; set; } public string EmailAddr […]

得到一个没有很多if语句的类

我有一定数量的类inheritance自抽象类: abstract public class baseClass { //logics } public class child1 : baseClass { } public class child2 : baseClass { } 现在我有一些管理类,必须创建这些类中的一个,具体取决于枚举,它将具有相同名称的值,如下所示: public enum ClassType { child1, child2 } public class Manager { private List _workers; public void Initialize(ClassType type) { //what logics to put here? (resulting in correctChild) _workers.Add(correctChild); } } 我在考虑使用typeof,但不知道如何实现它。 注意:在这个例子中它是2个类,但在实际情况下,它是任意数量的类。

C#中的属性或变量

可能重复: 属性与字段:需要帮助掌握属性对字段的使用。 我在课堂上看过属性和变量。 你能告诉我们我们使用哪种情况属性以及我们使用变量的场景。

决定使C#类静态

如果我在C#中有一个Utilities.cs类,它只包含静态方法 例如 public static string DoSomething() { return “something”; } 我应该让这个类本身是静态的吗? public static Utilities() { … 使类静态有什么优势吗?

通过接口使用静态类?

想象一下,您需要从整个应用程序中访问一些方法。 静态类是理想的。 public static class MyStaticClass { public static void MyMethod() { // Do Something here… } } 但也许将来我会在另一个静态类中添加静态方法的第二个实现。 public static class MyStaticClass2 { public static void MyMethod() { // Do Something here… } } 有没有办法改变我的其他代码中使用的静态类而不更改MyStaticClass.MeMethod();的调用MyStaticClass.MeMethod(); 到MyStaticClass2.MyMethod(); ? 我想到了一个界面,但我不知道如何实现这个……如果我说疯了就说出来,我只会改变电话:D

在C#中创建和使用自定义List

我正在尝试使用自定义列表,我已经添加了一些额外的工具。 我想将此列表应用于我创建的一长串自定义类。 所有类都有一个ID号,List中的一些工具使用ID。 这是我尝试使用的代码的一部分。 我希望这能帮助你理解我的问题。 namespace Simple_Point_of _Sales_System { public class MyList : List { internal int SetID() { return this.Max(n => n.ID) + 1; } internal T Find(int ID) { return this.Find(n => n.ID == ID); } internal T Add(T n) { Read(); Add(n); Write(); return n; } internal void Remove(int ID) { Read(); […]

循环通过类的常量成员

我有一个包含常量字符串的类。 我想将所有这些字符串放入下拉集合中。 做这个的最好方式是什么? 这就是我现在所拥有的,理论上,我认为这将是最好的方法。 public class TestClass { private const string _testA = “Test A”; private const string _testB = “Test B”; public string TestA { get { return _testA; } } public string TestB { get { return _testB; } } } public DropDownItemCollection TestCollection { DropDownItemCollection collection = new DropDownItemCollection(); TestClass class = […]

Lambda参数在后面的范围内访问字段时与类字段冲突

在名称方面我的想象力很弱,所以我经常发现自己在代码中重用了标识符。 这导致我遇到这个特定的问题。 这是一些示例代码: public delegate void TestDelegate(int test); public class Test { private int test; private void method(int aaa) { TestDelegate del = test => aaa++; test++; } public static void Main() { } } 以下是编译错误(由ideone输出): prog.cs(11,3): error CS0135: `test’ conflicts with a declaration in a child block prog.cs(9,22): (Location of the symbol related to […]

枚举在类体外但在命名空间内的定义

今天,我遇到了一些像这样的代码。 namespace Foo { public enum Game{ High, Low}; public enum Switch{On, Off}; public class Bar() { // Blah } } 我无法弄清楚它与声明课堂内的枚举之间的区别是什么。 AFAIK,你仍然可以“覆盖”课堂内的那些枚举。

单身类可以是静态的吗?

单身类可以是静态的吗?