具有内部 Clear() 方法的 C# 结构
我想要一个简单的商店信息结构。我的问题是创建清除存储值的内部方法。
public struct GraphValues
{
public int min, max, ...;
public GraphValues(int min, int max, ...)
{
this.min = min;
this.max = max;
...
}
public static void Clear(this GraphValues values)
{
values.min.Equals(null);
values.max.Equals(null);
...
}
}
所以在这个结构中写入新值是可以的,
GraphValues val = new GraphValues()
val.max = 12;
val.min = 1;
但我想将所有结构值设置为null。打电话有什么
val.Clear();
想法吗? 特纳克斯
I want to have simply structure for store information. My problem is to create inner method that clear stored values.
public struct GraphValues
{
public int min, max, ...;
public GraphValues(int min, int max, ...)
{
this.min = min;
this.max = max;
...
}
public static void Clear(this GraphValues values)
{
values.min.Equals(null);
values.max.Equals(null);
...
}
}
So write new values in this structure is OK,
GraphValues val = new GraphValues()
val.max = 12;
val.min = 1;
but I want to set all structure valueas to null. Calling something like
val.Clear();
Any idea?
Thnaks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
固定代码:
我还建议您考虑创建一个新对象是否比将现有对象上的字段设置为 null 更好:
Fixed code:
I'd also suggest that you consider if it would be better to create a new object instead of setting the fields on an existing object to null:
从
Clear()
方法定义中删除“static
”关键字,以便它可用于GraphValues
对象的每个实例。此外,无需传入参数 - 在Clear()
方法内,您将可以访问所有成员。并且您希望将它们设置为 null,而不是将它们的值与 null 进行比较。为什么这是一个结构体?只需使用类即可。
Remove the '
static
' keyword from theClear()
method definition, so that it's available for each instance of yourGraphValues
object. Also, no need to pass in parameters - inside theClear()
method you'll have access to all your members. And you want to set them to null, not compare their values to null.And why is this a struct anyway? Just use class.