Tag: generics

我使用通用限制来阻止特定类型

我有一个重载方法 – 第一个实现总是返回一个对象,第二个实现总是返回一个枚举。 我想使方法通用和重载,并限制编译器在generics类型可枚举时尝试绑定到非枚举方法… class Cache { T GetOrAdd (string cachekey, Func fnGetItem) where T : {is not IEnumerable} { } T[] GetOrAdd (string cachekey, Func<IEnumerable> fnGetItem) { } } 与…一起使用 { // The compile should choose the 1st overload var customer = Cache.GetOrAdd(“FirstCustomer”, () => context.Customers.First()); // The compile should choose the 2nd overload […]

C# – generics方法与非generics方法

我有点困惑为什么/何时我想要使用generics方法,因为非generics方法可以访问其包含类的generics成员并且无论如何都要传递generics参数。 所以,使用一个可能会忽略这一点的jar头示例(但突出了我为什么要问这个问题),为什么我会这样做: public class SomeGeneric { public T Swap(ref T a, ref T b) { T tmp = a; a = b; b = tmp; } } 过度 public class SomeGeneric { public T Swap(ref T a, ref T b) { T tmp = a; a = b; b = tmp; } } 这个? 或者,真的,为什么我要使用通用方法呢?

interface作为参数或generics方法与where – 有什么区别?

有什么区别: public void Method1(class1 c, T obj) where T:Imyinterface 和 public void Method2(class1 c, Imyinterface obj) ? 使用第一种方法有什么好处?

将返回字符分隔的字符串转换为List 的最佳方法是什么?

我需要经常将“字符串块” (包含返回字符的字符串,例如从文件或TextBox) 转换为List 。 比下面的ConvertBlockToLines方法更优雅的方法是什么? using System; using System.Collections.Generic; using System.Linq; namespace TestConvert9922 { class Program { static void Main(string[] args) { string testBlock = “line one” + Environment.NewLine + “line two” + Environment.NewLine + “line three” + Environment.NewLine + “line four” + Environment.NewLine + “line five”; List lines = StringHelpers.ConvertBlockToLines(testBlock); lines.ForEach(l => Console.WriteLine(l)); Console.ReadLine(); […]

在C#Dictionary 中设置所有值的最佳方法是什么?

在C#字典中设置所有值的最佳方法是什么? 这是我现在正在做的,但我确信有更好/更清洁的方法来做到这一点: Dictionary dict = GetDictionary(); var keys = dict.Keys.ToList(); for (int i = 0; i < keys.Count; i++) { dict[keys[i]] = false; } 我已经尝试过foreach的其他方法,但我有错误。

如何创建单例IEnumerable?

C#是否提供了一种很好的方法来将类型T的单个实体IEnumerable为IEnumerable ? 我能想到的唯一方法是: T entity = new T(); IEnumerable = new List { entity }.AsEnumerable(); 我想应该有更好的方法。

将通用字典转换为不同的类型

有没有一种快速的方法将通用字典从一种类型转换为另一种类型 我有这个 IDictionary _commands; 并且需要将它传递给一个带有稍微不同的类型字典的函数 public void Handle(IDictionary _commands);

我可以制作一个通用的可选项,默认为某个类吗?

我的问题与C#generics中“默认”类型参数的合理方法有关吗? ,但使用内部generics类,该方法不起作用。 给出这样的代码: using System; public class FooEventArgs : EventArgs { // … T properties and a constructor } public class Foo { public delegate void EventHandler(object sender, FooEventArgs e); public event EventHandler<FooEventArgs> Changed } 并使用它像这样: public class User { public Foo foo1; public Foo foo2; public User() { foo1 = new Foo(); foo2 = […]

所有C#generics实例都是常见的静态成员变量吗?

在C#中我有一个generics类: public class MyGeneric where ParameterClass: MyGenericParameterClass, new() { public static int Variable; } 现在在C ++中如果我实例化一个带有不同参数的模板化类,每个完整的类都会得到它自己的Variable ,所以我就是说不出来 MyGeneric.Variable = 1; // invalid in C++ 在C ++中,但似乎我可以在C#中这样做。 我想澄清一下…… 如果我有一个带有静态成员变量的generics,那个变量是否在所有generics实例之间共享?

具有generics类型的名称

我试图在通用接口上获取方法的名称。 我希望这可以工作,因为类型部分将是一个有效的类型: //This does not compile nameof(IGenericInterface.Method) //This would compile typeof(IGenericInterface) 我认为这应该是有效的C#6或我错过了什么或者是他们更好的方法来做到这一点。 我不想为方法名称使用字符串,就像重命名方法一样,代码会在没有任何构建时错误的情况下中断。