Tag: class

为什么C#中的类没有循环布局问题?

public struct Unit { Unit u; } 原因: 类型为“单元”的结构成员“Unit.u”会在结构布局中生成一个循环。 但 public class Unit { Unit u; } 编译。 我理解我认为的问题。 引用Unit对象时将形成无限循环,因为它必须初始化另一个成员Unit ,依此类推。 但是为什么编译器只为structs限制问题呢? 这个问题也不存在于class吗? 我错过了什么吗?

如何在C#中将类作为IEnumerable?

所以我在其中有一个类和一个通用List,但它是私有的。 class Contacts { List contacts; … } 我想让这个课有效: foreach(Contact in contacts) …. ; 像这样(不工作): Contacts c; foreach(Contact in c) …. ; 在上面的示例中,Contact类实例c必须返回联系人的每个项目(c的私有成员) 我该怎么做? 我知道它必须是IEnumerable与yield return,但在哪里声明?

c类中的类的getter和setter

假设我们有一个带有属性和getter / setter的类InnerClass。 我们还有一个包含InnerClass的类OuterClass。 例如 class InnerClass { private int m_a; private int m_b; public int M_A { get { return m_a; } set { m_a = value; } } } class OuterClass { private InnerClass innerClass } 我如何为OuterClass的innerClass成员实现正确的getter和setter? 提前致谢!

使用AttributeTargets.Class对自定义ValidationAttribute进行客户端validation

是否可以为Class范围中使用的自定义ValidationAttribute实现客户端站点validation? 例如我的MaxLengthGlobal,它应该确保所有输入字段的全局最大限制。 [AttributeUsage(AttributeTargets.Class)] public class MaxLengthGlobalAttribute : ValidationAttribute, IClientValidatable { public int MaximumLength { get; private set; } public MaxLengthGlobalAttribute(int maximumLength) { this.MaximumLength = maximumLength; } public override bool IsValid(object value) { var properties = TypeDescriptor.GetProperties(value); foreach (PropertyDescriptor property in properties) { var stringValue = property.GetValue(value) as string; if (stringValue != null && (stringValue.Length > […]

在另一个.cs文件类中使用一个.cs文件类中的方法

我有2个.cs文件,每个文件都有一个类。 如何从Form2.cs中另一个类的Form1.cs中调用类中的方法? 看起来像这样…… Form1.cs public partial class Class1 : ClassContainer { public void awesomeMethod() { } } Form2.cs class Class2 : SomethingChanged { public void decentMethod() { } } 我想在decentMethod()中调用awesomeMethod()。 谢谢。

将T限制为字符串和整数?

我已经为自己构建了一个generics集合类,它是这样定义的。 public class StatisticItemHits{…} 此类只能与int和string值一起使用。 不过这个 public class StatisticItemHits where T : string, int {…} 不会编译。 我究竟做错了什么?

不包含带2个参数的构造函数?

我目前正在做一些类编码,并想知道我的项目出了什么问题? class ContactPerson { string name; ContactNo telNo; public ContactPerson(string in_Name, ContactNo in_No) { name = in_Name; telNo = new ContactNo(); } public string getName() { return name; } public ContactNo getContactInfo() { return telNo; } public void setName(string in_Name) { name = in_Name; } public void setContactInfo (ContactNo in_No) { telNo = in_No; } […]

并非所有代码路径都在类中返回值

我知道这个问题曾被问到过,但我无法解决这个问题 我想编写一个类来检测并返回颜色但是我得到了这个错误:并非所有代码路径都在类中返回一个值 class Class1 { public System.Drawing.Color colordetector(string name) { if (name.Contains(“blue”) == true) { return System.Drawing.Color.Blue; } if (name.Contains(“green”) == true) { return System.Drawing.Color.Green; } if (name.Contains(“red”) == true) { return System.Drawing.Color.Red; } }//method colordetector }//class1

如何引用从孩子到父母的课程?

这可能听起来很愚蠢,但我如何从父母的另一个脚本中的一个脚本中引用一个类? 我在谷歌上找不到任何东西。 注意:我的脚本中有几个错误,这不是post的重点。 //Public //Private private Rigidbody myRigidbody; private Renderer myRenderer; private Material tileDefaultMaterial; private Material tileSelectedMaterial; private Material tileSameGroupMaterial; void Start () { myRigidbody = GetComponent (); myRenderer = GetComponent (); tileDefaultMaterial = Resources.Load (“TileDefault”, typeof(Material)) as Material; tileSelectedMaterial = Resources.Load (“TileSelected”, typeof(Material)) as Material; tileSameGroupMaterial = Resources.Load (“TileSameGroup”, typeof(Material)) as Material; } void […]

如何删除类的实例?

所以,我有这个项目创建一个类的多个实例,并列出它们。 在某些时候,不再需要实例化的类。 我怎么冲洗它? 到目前为止,它不会发生,即使该类不再需要做任何事情(如果它是一个静态类,程序将关闭),它仍然在我的列表中,它的公共变量仍然可用。 ..