具有内部 Clear() 方法的 C# 结构

发布于 2024-12-06 02:18:33 字数 665 浏览 5 评论 0原文

我想要一个简单的商店信息结构。我的问题是创建清除存储值的内部方法。

   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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓝色星空 2024-12-13 02:18:33
  1. 结构不应该是可变的。让你的类型成为一个类。
  2. 要允许 null 值,请使用可为 null 的类型。
  3. 使用属性而不是公共字段。

固定代码:

public class GraphValues
{
     public int? Min { get; set; } 
     public int? Max { get; set; }
     // etc...

     public void Clear()
     {
         Min = null;
         Max = null;
         // etc...
     }
}

我还建议您考虑创建一个新对象是否比将现有对象上的字段设置为 null 更好:

//val.Clear();           // Don't do this.
val = new GraphValues(); // Just create a new object.
  1. Structs should not be mutable. Make your type a class instead.
  2. To allow null values, use nullable types.
  3. Use properties instead of public fields.

Fixed code:

public class GraphValues
{
     public int? Min { get; set; } 
     public int? Max { get; set; }
     // etc...

     public void Clear()
     {
         Min = null;
         Max = null;
         // etc...
     }
}

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:

//val.Clear();           // Don't do this.
val = new GraphValues(); // Just create a new object.
凉月流沐 2024-12-13 02:18:33

Clear() 方法定义中删除“static”关键字,以便它可用于 GraphValues 对象的每个实例。此外,无需传入参数 - 在 Clear() 方法内,您将可以访问所有成员。并且您希望将它们设置为 null,而不是将它们的值与 null 进行比较。

    public void Clear()
    {
        this.min = null;
        this.max = null;
        ...
    }

为什么这是一个结构体?只需使用类即可。

Remove the 'static' keyword from the Clear() method definition, so that it's available for each instance of your GraphValues object. Also, no need to pass in parameters - inside the Clear() method you'll have access to all your members. And you want to set them to null, not compare their values to null.

    public void Clear()
    {
        this.min = null;
        this.max = null;
        ...
    }

And why is this a struct anyway? Just use class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文