相关疑难解决方法(0)

为什么C#不能从这个看似简单明显的案例中推断出类型

鉴于此代码:

class C
{
    C()
    {
        Test<string>(A); // fine
        Test((string a) => {}); // fine
        Test((Action<string>)A); // fine

        Test(A); // type arguments cannot be inferred from usage!
    }

    static void Test<T>(Action<T> a) { }

    void A(string _) { }
}
Run Code Online (Sandbox Code Playgroud)

编译器抱怨Test(A)无法弄清楚Tstring.

这对我来说似乎是一个非常简单的案例,我发誓我在其他通用实用程序和扩展函数中依赖于更复杂的推理.我在这里错过了什么?

更新1:这是在C#4.0编译器中.我在VS2010中发现了这个问题,上面的示例来自我在LINQPad 4中制作的最简单的repro.

更新2:在可用的列表中添加了更多示例.

c#

67
推荐指数
3
解决办法
3303
查看次数

为什么C#不支持类构造函数中隐含的泛型类型?

如果编译器可以推断它,C#不要求您指定泛型类型参数,例如:

List<int> myInts = new List<int> {0,1,1,
    2,3,5,8,13,21,34,55,89,144,233,377,
    610,987,1597,2584,4181,6765};

//this statement is clunky
List<string> myStrings = myInts.
    Select<int,string>( i => i.ToString() ).
    ToList<string>();

//the type is inferred from the lambda expression
//the compiler knows that it's taking an int and 
//returning a string
List<string> myStrings = myInts.
    Select( i => i.ToString() ).
    ToList();
Run Code Online (Sandbox Code Playgroud)

这是匿名类型所必需的,你不知道类型参数是什么(在intellisense中它显示为'a),因为它是由编译器添加的.

类级别类型参数不允许您这样做:

//sample generic class
public class GenericDemo<T> 
{
    public GenericDemo ( T value ) 
    {
        GenericTypedProperty = value;
    }

    public T GenericTypedProperty {get; set;} …
Run Code Online (Sandbox Code Playgroud)

.net c# generics

49
推荐指数
2
解决办法
2万
查看次数

C#构造函数泛型参数推断

为什么C#推断方法的泛型参数而不是构造函数?

new Tuple<int, int>(5, 5)Tuple.Create(5, 5)

c# generics type-inference tuples

11
推荐指数
2
解决办法
3631
查看次数

使用基类IEqualityComparer做Distinct(),仍然返回子类类型?

我有许多是从类派生的类BaseClass,其中BaseClass仅仅有一个'ID属性.

我现在需要对其中一些对象的集合进行区分.我为每个子类反复提供以下代码:

public class PositionComparer : IEqualityComparer<Position>
{
    public bool Equals(Position x, Position y)
    {
        return (x.Id == y.Id);
    }

    public int GetHashCode(Position obj)
    {
        return obj.Id.GetHashCode();
    }
}
Run Code Online (Sandbox Code Playgroud)

鉴于逻辑只是基于Id,我想创建一个单一的比较器来减少重复:

public class BaseClassComparer : IEqualityComparer<BaseClass>
{
    public bool Equals(BaseClass x, BaseClass y)
    {
        return (x.Id == y.Id);
    }

    public int GetHashCode(BaseClass obj)
    {
        return obj.Id.GetHashCode();
    }
}
Run Code Online (Sandbox Code Playgroud)

但这似乎没有编译:

  IEnumerable<Position> positions = GetAllPositions();
  positions = allPositions.Distinct(new BaseClassComparer())
Run Code Online (Sandbox Code Playgroud)

...因为它说它无法转换BaseClassPosition.为什么比较器会强制执行此Distinct()调用的返回值?

c# linq distinct iequalitycomparer

7
推荐指数
2
解决办法
1080
查看次数

多个构造函数与静态Create方法

作为"javaland"程序员,我习惯于工厂方法和多个构造函数.我对工厂模式的主要用途是将决策延迟到运行时,在实例化期间执行某种副作用或限制或隐藏具体类型.当我深入研究C#时,我看到越来越多的API混合了多个构造函数和静态Create方法.

例如,XmlReaderXmlTextReader.XmlWriterXmlTextWriter.

我的问题是:

  1. Create方法有什么特别的东西,或者它只是一个约定(比如java :) getInstance
  2. 关于工厂方法与构造函数的C#有哪些好的做法?例如,为什么有几个Create方法接受XmlWriterSettings参数XmlWriter而没有构造函数XmlTextWriter具有相同的目的?而另一方面,为什么只有构造函数接受Encoding参数呢?
  3. 我想主要的问题是,在惯用的C#中,何时建议公开工厂方法以及何时公开构造函数?

c# oop

5
推荐指数
1
解决办法
2084
查看次数

为什么构造函数中不允许使用类型参数?

方法有类型参数.为什么构造函数没有类型参数?

我认为有几个(不是很多)例子可供它们使用.我目前的问题如下:

internal class ClassA
{
   private readonly Delegate _delegate;

   public ClassA<T>(Func<T> func)
   {
     _delegate = func;
   }
}
Run Code Online (Sandbox Code Playgroud)

A Delegate对我的班级来说已经足够了.但要将其作为方法组传递,我需要将参数定义为a Func<T>.

c#

5
推荐指数
1
解决办法
309
查看次数

将List <anonymous type>传递给C#中需要List <T>的方法

这可能吗?

var cats = catsource.Select(c => new
{
    CatId = c.catId,
    Name = c.Name,
}).ToList();
Run Code Online (Sandbox Code Playgroud)

var myCatObject = new CatData(cats);

这不起作用,因为编译器说它无法转换catsList<T>.

cats是一种匿名类型,CatData是:

public class CatData<T>
{
    public int total;
    public int records;
    public List<T> records;

    public CatData(List<T> dataObjects, int totalRecords, int numberOfRows)
    {
        total = (int)Math.Ceiling((double)totalRecords / numberOfRows);
        records = totalRecords;
        rows = dataObjects;
    }
}
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?


编辑:

所以我已经完成了这个代码,但仍然存在一个问题:

 public class CatData
 {
     public int total;
     public int records;
     public List<T> rows;

     private …
Run Code Online (Sandbox Code Playgroud)

c# generics

2
推荐指数
1
解决办法
613
查看次数

标签 统计

c# ×7

generics ×3

.net ×1

distinct ×1

iequalitycomparer ×1

linq ×1

oop ×1

tuples ×1

type-inference ×1