任何人都可以在C#界面中使用静态方法吗? [英] Anyone has an alternative to using static methods in a C# interface?

查看:117
本文介绍了任何人都可以在C#界面中使用静态方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现一个集合,其项目需要进行空虚测试。
如果是引用类型,则会测试为null。对于值类型,必须实现空测试,并且可能选择表示空白的特定值。

I want to implement a collection, whose items need to be tested for emptiness. In case of a reference type, one would test for being null. For value types, one has to implement empty testing, and probably choose a specific value that represents emptyness.

我的T的通用集合应该可用于值和引用类型值(意味着 Coll< MyCalss> Coll< int> 应该都可以)。但是我必须以不同的方式测试引用和值类型。

My generic collection of T should be usable for both value and reference type values (meaning that Coll<MyCalss> and Coll<int> should both be possible). But I have to test reference and value types differently.

拥有一个实现IsEmpty()方法的接口,从我的方法中排除这个逻辑不是很好通用型?但是,当然,这个IsEmpty()方法不能是成员函数:它无法在空对象上调用。

Wouldn't it be nice to have an interface that implements an IsEmpty() method, to exclude this logic from my generic type? But of course, this IsEmpty() method cannot be a member function: it could not be called on empty objects.

我找到的一个解决方法是将集合项存储为对象,而不是Ts,但它让我头疼(围绕拳击和强烈打字)。在旧的C ++中没有问题: - )

One workaround I found is to have collection items stored as objects, rather then T-s, but it gives me a headache (around boxing and being strongly typed). In good old C++ it was no problem :-)

下面的代码演示了我想要实现的目标:

The code below demonstrates what I'd like to achieve:

using System;
using System.Collections.Generic;

namespace StaticMethodInInterfaceDemo
{
  public interface IEmpty<T>
  {
    static T GetEmpty();  // or static T Empty;

    static bool IsEmpty(T ItemToTest);
  }


  public class Coll<T> where T : IEmpty<T>
  {
    protected T[] Items;

    protected int Count;


    public Coll(int Capacity)
    {
      this.Items = new T[Capacity];
      this.Count = 0;
    }

    public void Remove(T ItemToRemove)
    {
      int Index = Find(ItemToRemove);

      // Problem spot 1: This throws a compiler error: "Cannot convert null to type parameter 'T'
      // because it could be a non-nullable value type. Consider using 'default(T)' instead."
      this.Items[Index] = null;

      // To overcome, I'd like to write this:
      this.Items[Index] = T.Empty;  // or T.GetEmpty(), whatever.

      this.Count--;
    }


    public T[] ToArray()
    {
      T[] ret = new T[this.Count];

      int TargetIndex = 0;
      for(int Index = 0; Index < this.Items.GetLength(0); Index++)
      {
        T Item = this.Items[Index];

        // Problem spot 2: This test is not correct for value types.
        if (Item != null)
          ret[TargetIndex++] = Item;

        // I'd like to do this:
        if (!T.IsEmpty(Item))
          ret[TargetIndex++] = Item;
      }

      return ret;
    }

    protected int Find(T ItemToFind)
    {
      return 1;  // Not implemented in the sample.
    }
  }
}


推荐答案

您可以为名为IsEmpty的接口创建扩展方法。然后你可以先测试这个扩展方法中的'this'参数是否为null。

You can create an extension method for your Interface called IsEmpty. Then you could first test if the 'this' parameter in this extension method is null.

所以你可以在对实现你的类型的任何引用上调用IsEmpty方法接口,而不是关于它是否为null。

So you could call the IsEmpty method on any reference to a type that is implementing your interface, not regarding if this could be null or not.

这篇关于任何人都可以在C#界面中使用静态方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆