【问题标题】:How would I return N number of IEnumerable<bool> value with a single method in C#?如何在 C# 中使用单个方法返回 N 个 IEnumerable<bool> 值?
【发布时间】:2021-12-02 23:46:59
【问题描述】:

任务:

  • 使用一个名为 IBoolS 的方法创建一个名为 IBoolS 的公共接口 GetBools:参数int N,返回值IEnumerable。

  • 创建一个名为 Generator 的公共类来实现这个接口 具有名为 ValueToGenerate 的布尔属性和 GetBools 这里的方法应该返回N个ValueToGenerate。

这是我迄今为止尝试过的:

IBoolS.cs:

public interface IBoolS
{
   public IEnumerable<bool> GetBools(int N)
    {
        //what goes here? yield return something, but what?
    }
}

Generator.cs:

public class Generator : IBoolS
{
    private bool ValueToGenerate;

    //GetBools(N pieces of ValueToGenerate);, but should be an integer number
}

【问题讨论】:

  • 没有任何东西进入界面。它只是定义了方法的签名,没有别的
  • @ThomasWeller 从 C# 8.0 开始就有可能(默认实现)
  • 也许吧。这个任务看起来像一个编程作业,侧重于早期学期的编程语言知识。 @Akuhei,如果您想了解大多数现代 C# 8.0 的内容,请提及。
  • @SirRufo 虽然它是真的(自 C# 8.0 起),但我仍然不使用它,也不推荐使用它。在我看来(并且要有一个不混淆的约定),接口应该只是契约,如果需要,默认实现可以进入抽象或非抽象基类。
  • 不,这真的只是一个编程作业。那么我应该在界面中只写这个吗?公共 IEnumerable GetBools(int N);

标签: c# .net ienumerable yield-return


【解决方案1】:

接口只是一个声明,是实现它的类的契约

public interface IBoolS
{
   IEnumerable<bool> GetBools(int N);
}

class实现接口:

public class Generator : IBoolS
{
    // Let's have a property instead of field: which value is generated
    public bool ValueToGenerate {get;}

    // Do not forget a constructor - we have to set ValueToGenerate
    public Generator(bool valueToGenerate) {
      ValueToGenerate = valueToGenerate;
    }    

    // Interface implementation: we generate N bool values
    public IEnumerable<bool> GetBools(int N) {
      // Input validation
      if (N < 0)
        throw new ArgumentOutOfRangeException(nameof(N));

      // Shorter version is
      // return Enumerable.Repeat(ValueToGenerate, N);
      for (int i = 0; i < N; ++i)
        yield return ValueToGenerate; 
    } 
} 

【讨论】:

  • nameof('N') 是语法错误。也许你的意思是nameof(N)
  • @Blindy:谢谢!我很抱歉错字
  • 为什么是 ++i 而不是 i++?
  • @Aksi: ++i 而不是 i++ 我的旧习惯:在的时候,C 编译器、源代码和 exe 必须适合 640k 优化器 需要帮助,++ifor 循环中i++ 更快。现在,优化器已经足够聪明了,++ii++ 是相等的
  • 啊好的 :) 谢谢!
【解决方案2】:

听起来你只需要Enumerable.Repeat

public interface IBoolS
{
   IEnumerable<bool> GetBools(int N);
}

public class Generator : IBoolS
{
    public bool ValueToGenerate { get; set; }

    public IEnumerable<bool> GetBools(int N) => Enumerable.Repeat(ValueToGenerate, N);
}

【讨论】:

  • 旁注:public bool ValueToGenerate { get; init; },请注意init;,而不是set;,如果我们想要不可变版本,则可以选择
【解决方案3】:

这是一个可能的实现(虽然不是高性能的):

public interface IBoolS
{
    IEnumerable<bool> GetBools(int N);
}

public class Generator : IBoolS
{
    private bool _valueToGenerate;
    
    public Generator(bool valueToGenerate) {
        _valueToGenerate = valueToGenerate;
    }

    public IEnumerable<bool> GetBools(int N) {
        foreach (var n in Enumerable.Range(0, N)) {
            yield return _valueToGenerate;
        }                        
    }
}

【讨论】:

    猜你喜欢
    • 2014-05-27
    • 2019-01-02
    • 1970-01-01
    • 2016-02-01
    • 2013-06-18
    • 1970-01-01
    • 2016-07-07
    • 1970-01-01
    • 2023-03-31
    相关资源
    最近更新 更多