简体   繁体   English

C#:为什么当我们不指定泛型时,泛型不使用它可以使用的最泛型类型?

[英]C#: Why don't generics use the most generic type it can when we don't specify one?

For example I now created a this tiny class: 例如,我现在创建了一个很小的类:

public static class FileSystemInfoComparers<T> where T : FileSystemInfo
{
    public static IEqualityComparer<T> FullName
    {
        get { return new FullNameComparer(); }
    }

    private class FullNameComparer : IEqualityComparer<T>
    {
        public bool Equals(T x, T y)  { return x.FullName == y.FullName;   }
        public int GetHashCode(T obj) { return obj.FullName.GetHashCode(); }
    }
}

I would like it if I could just do 如果可以的话我会想要的

var comparer = FileSystemInfoComparers.FullName;

and have an instance of IEqualityComparer<FileSystemInfo> , since I didn't specify any type and FileSystemInfo is the most generic type T can be. 并具有IEqualityComparer<FileSystemInfo>的实例,因为我未指定任何类型,并且FileSystemInfo是T可以使用的最通用的类​​型。 With no type constraint the default type could for example be object or something. 在没有类型约束的情况下,默认类型例如可以是对象或某种东西。

Maybe not the best example, but anyways just got curious here :p 也许不是最好的例子,但是反正只是在这里感到好奇:p

Sounds like a recipe for trouble to me. 听起来像是困扰我的良方。

In particular, you could easily create a non-generic class called FileSystemInfoComparers with a static property of the same name, and suddenly your code would mean something completely different. 特别是,您可以轻松地创建一个具有相同名称的静态属性的名为FileSystemInfoComparers的非泛型类,突然,您的代码将意味着完全不同的东西。

I'd rather keep things simple. 我宁愿保持简单。 (Generics are complicated enough already, and type inference in particular is pretty hairy.) (泛型已经足够复杂了,尤其是类型推断是很麻烦的。)

That is an interesting idea and it could definitely work but consider that it would only work in cases where the generic type argument is constrained with a concrete type. 这是一个有趣的想法,它肯定可以工作,但是考虑到它仅在泛型类型参数受具体类型约束的情况下才有效。

The .NET compilers are very good at type inference but tend to shy away from making any assumptions. .NET编译器非常擅长类型推断,但往往会避开任何假设。 I don't see any reason why this couldn't be done except that it would only work in a small number of highly-specific instances. 除了只能在少数高度特定的实例中工作之外,我看不到其他任何原因无法完成。 Since it has no general purpose I would imagine that Microsoft would be less inclined to make a change to support it. 由于它没有通用的目的,我可以想象微软将不太愿意进行更改以支持它。

You could possible do this by having a helper class that has the default type you want set. 您可以通过设置具有您要设置的默认类型的帮助器类来实现此目的。 But what you are asking for currently cannot be done in C# 3.0. 但是,您目前要求的内容无法在C#3.0中完成。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么 C# 不接受带有 generics 参数的构造函数要求? - Why C# don't accept constructor requirements with parameters on generics? C#泛型 - 为什么lambda工作,简单方法不工作? - C# generics — why do lambdas work, when simple methods don't? 为什么泛型结构不能具有在 C# 中指定泛型类型的静态成员? - Why can't generic structs have static members which specify the generic type in C#? 如何在C#泛型中指定可以从字符串构造的T? (通用类型约束) - How to specify in C# generics such T that is constructible from string? (generic type constraint) 为什么在 EF Core 中使用 Include(或 ThenInclude)时不需要指定类型? - Why don't we need to specify types when using Include ( or ThenInclude) with EF Core? C#泛型:我可以约束一组没有实现接口的类吗? - C# Generics: Can I constrain to a set of classes that don't implement an interface? 为什么当我使用RegisterStartupScript或RegisterClientScriptBlock时,我的C#代码不起作用? - Why when I use RegisterStartupScript or RegisterClientScriptBlock my C# code don't works? C#为什么StackPanel不调整大小? - c# Why stackpanel don't resize? 为什么我不能用T? 作为 C# 中的泛型参数 - Why I can't use T? as a generic parameter in C# 如果我没有使用CSharpCodeProvider指定CompilerVersion,为什么大多数样本都指定它,会发生什么? - What happens if I don't specify CompilerVersion with CSharpCodeProvider and why do most samples specify it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM