English 中文(简体)
C# 推论类型: 如何适当界定对方法的限制?
原标题:C# Type inference: How to properly define constraints on method?

然而,我花了很多时间,使C#类推论达到我所希望的目的。 我有一个非常具体的情况,即我拥有大量变数。

ConcurrentDictionary<T, IDictionary<U, V> >

如果T,U,V可以是一些任意的,如长期、隐蔽或任何。

我想写出与这些变量有关的方法,特别是审查这些变量的图表。

因此,我写了一种方法。

public static IOrderedEnumerable<Tuple<int,int>> GetDictionaryHistogram<T, U, V, W>(T dictionary) where T : ConcurrentDictionary<U, IDictionary<V, W>>
{
    return dictionary.Select(p => p.Value.Count)
                     .GroupBy(p => p)
                     .Select(p => new Tuple<int, int>(p.Key, p.Count()))
                     .OrderBy(p => p.Item1);
}

But when I try to call it, C# gives me an error that it cannot infer the types. For example on a variable of type

ConcurrentDictionary<int,IDictionary<int, int> > foo;

我发现错误:

Error 118 The type arguments for method Auditor.AuditorHelpers.GetDictionaryHistogram(T) cannot be inferred from the usage. Try specifying the type arguments explicitly.

我有什么错误?

最佳回答

arguments to ual paranote category。 在constraints上从未作过任何推论,因为制约因素不属于一种方法的signature的一部分。

In your case type inference must always fail; type inference cannot possibly infer types for U and V because they do not appear in a formal parameter type.

For about a dozen people telling me that I am wrong to believe that this rule is sensible, see the comments to my article on the subject.

问题回答

The type parameter T is unnecessary. You should just write

public static IOrderedEnumerable<Tuple<int,int>> GetDictionaryHistogram<U, V, W>(
    ConcurrentDictionary<U, IDictionary<V, W>> dictionary)

并且应当处以罚款。

你的制约因素类型,即参数必须是某种类型,只在少数情况下才有用。 最常见的是避免采用接口的价值类型的盒式。 比较这两种方法:

public static DoSomething(ISomeInterface thing) {}
public static DoSomething<T>(T thing) where T : ISomeInterface {}

当第一种方法被称作具有价值类型论点时,该论点被打上箱子,并投到接口类型上。

当第二种方法被称作具有价值类型时,一般类型参数被替换为价值类型,没有发生任何箱子。

I think the compiler is saying that it can t figure out the dependencies between your types.

Try doing:

public static IOrderedEnumerable<Tuple<int,int>> GetDictionaryHistogram<U, V, W>(ConcurrentDictionary<U, IDictionary<V, W>> dictionary)
{
    return dictionary.Select(p => p.Value.Count)
                     .GroupBy(p => p)
                     .Select(p => new Tuple<int, int>(p.Key, p.Count()))
                     .OrderBy(p => p.Item1);
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签