小编Evg*_*hin的帖子

为什么编译可以,当我使用 Invoke 方法时,当我直接返回 Func<int,int> 时不可以?

我不明白这个案例:

public delegate int test(int i);

public test Success()
{
    Func<int, int> f = x => x;
    return f.Invoke; // <- code successfully compiled 
}

public test Fail()
{
    Func<int, int> f = x => x;
    return f; // <- code doesn't compile
}
Run Code Online (Sandbox Code Playgroud)

为什么用Invokemethod编译可以,csharp Func<int,int>直接返回就不行?

c# lambda delegates

29
推荐指数
2
解决办法
801
查看次数

为什么嵌套类中的隐式运算符方法无法编译?

这段代码给出了一个错误:

public class A<T>
    {
        public class B<T1> : A<T1>
        {
            public static implicit operator bool(B<T1> b) => true;
        }
    }

Run Code Online (Sandbox Code Playgroud)

但是,如果我将类分开,则没有错误:


  public class A<T> {     }

  public class B<T> : A<T>
  {
       public static implicit operator bool(B<T> b) => true;
  }
Run Code Online (Sandbox Code Playgroud)

c# generics implicit inner-classes

6
推荐指数
1
解决办法
101
查看次数

为什么在 IL 代码中找不到委托的 Invoke 方法体?

我反编译了我的源代码 TestDelegate

public delegate int TestDelegate(int a, int b);
Run Code Online (Sandbox Code Playgroud)

在查看这个IL代码时,为什么找不到Invoke方法?我也无法在委托中找到其他方法。它是如何工作的?

  .method public hidebysig virtual newslot instance int32
    Invoke(
      int32 a,
      int32 b
    ) runtime managed
  {
    // Can't find a body
  } // end of method TestDelegate::Invoke
Run Code Online (Sandbox Code Playgroud)
  .method public hidebysig virtual newslot instance int32
    Invoke(
      int32 a,
      int32 b
    ) runtime managed
  {
    // Can't find a body
  } // end of method TestDelegate::Invoke
Run Code Online (Sandbox Code Playgroud)

伊尔:

    IL_001c: callvirt     instance int32 Resolvers.Tests.Delegates.TestDelegate::Invoke(int32, int32)

Run Code Online (Sandbox Code Playgroud)

生成IL显示Invoke方法调用,我没找到。到底发生了什么?

c# delegates cil

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

如何计算浮点型精度,是否有意义?

我在理解浮点类型的精度时遇到问题。msdn将该精度写入6 到 9 位数字。但我注意到精度取决于数字的大小:

  float smallNumber = 1.0000001f;
  Console.WriteLine(smallNumber); // 1.0000001

  bigNumber = 100000001f;
  Console.WriteLine(bigNumber); // 100000000

Run Code Online (Sandbox Code Playgroud)

smallNumber 比 big 更精确,我了解 IEEE754,但我不明白 MSDN 如何计算精度,这是否有意义?

此外,您可以在此处使用浮点格式的数字表示。请在“您输入”输入中输入 100000000 值,然后单击右侧的“+1”。然后将输入的值更改为 1,并再次单击“+1”。您可能会看到精度上的差异。

c# math floating-point ieee-754

0
推荐指数
1
解决办法
291
查看次数