小编Set*_*o N的帖子

为什么不能在同一方法中使用"返回"和"收益率"?

为什么我们不能在同一方法中同时使用return和yield return?

例如,我们可以在下面使用GetIntegers1和GetIntegers2,但不能使用GetIntegers3.

public IEnumerable<int> GetIntegers1()
{
  return new[] { 4, 5, 6 };
}

public IEnumerable<int> GetIntegers2()
{
  yield return 1;
  yield return 2;
  yield return 3;
}

public IEnumerable<int> GetIntegers3()
{
  if ( someCondition )
  {
    return new[] {4, 5, 6}; // compiler error
  }
  else
  {
    yield return 1;
    yield return 2;
    yield return 3;
  }
}
Run Code Online (Sandbox Code Playgroud)

c# ienumerable return yield-return

21
推荐指数
3
解决办法
5189
查看次数

为什么在构造函数中抛出异常导致空引用?

为什么在构造函数中抛出异常导致空引用?例如,如果我们运行下面的代码,则teacher的值为null,而st.teacher则不是(创建了Teacher对象).为什么?

using System;

namespace ConsoleApplication1
{
  class Program
  {
    static void Main( string[] args )
    {
      Test();
    }

    private static void Test()
    {
      Teacher teacher = null;
      Student st = new Student();
      try
      {
        teacher = new Teacher( "", st );
      }
      catch ( Exception e )
      {
        Console.WriteLine( e.Message );
      }
      Console.WriteLine( ( teacher == null ) );  // output True
      Console.WriteLine( ( st.teacher == null ) );  // output False
    }
  }

  class Teacher
  {
    public string name; …
Run Code Online (Sandbox Code Playgroud)

c# null constructor exception object

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

为什么Stack <T>和Queue <T>在List <T>时没有容量属性?

Capacity属性在List中比在Stack和Queue等其他集合中更有用吗?或者有另一种方法来获得堆栈或队列的容量?

c# queue stack list capacity

12
推荐指数
1
解决办法
3100
查看次数

使用括号分配匿名方法进行委托会产生编译错误?

给出以下示例代码:

static void SomeMethod()
{
  Action<int,int> myDelegate;

  //...

  myDelegate = delegate { Console.WriteLine( 0 ); };
  myDelegate = delegate() { Console.WriteLine( 0 ); };  // compile error

}
Run Code Online (Sandbox Code Playgroud)

有什么区别

myDelegate = delegate { Console.WriteLine( 0 ); };
Run Code Online (Sandbox Code Playgroud)

myDelegate = delegate() { Console.WriteLine( 0 ); };
Run Code Online (Sandbox Code Playgroud)

在此示例中,第二个语句生成编译错误,而第一个语句则不生成编译错误.

c# delegates anonymous-methods

9
推荐指数
1
解决办法
1112
查看次数

取消订阅从未订阅过的活动是否安全?

例如,如果这些代码:

        Button button1 = new Button();
        // ...
        button1.Click -= button1_Clicked;
Run Code Online (Sandbox Code Playgroud)

在执行之前执行:

        button1.Click += button1_Clicked;
Run Code Online (Sandbox Code Playgroud)

我没有发现错误或异常,但我想知道这里是否有任何缺点.

如果它是安全的,为什么允许取消订阅从未订阅过的活动?

.net c# events unsubscribe subscribe

9
推荐指数
1
解决办法
4621
查看次数

即使未移动鼠标,PictureBox中的MouseMove事件也会连续触发

为什么即使鼠标没有移动,PictureBox的MouseMove事件似乎也会连续触发?我已经尝试了以下代码来证明它(通过简单地创建一个带有PictureBox和Label的新表单).

private void pictureBox1_MouseMove ( object sender, MouseEventArgs e )
{
  label1.Text = DateTime.Now.ToLongTimeString ( ) + ": " + e.X + "," + e.Y;
}
Run Code Online (Sandbox Code Playgroud)

.net c# picturebox mousemove winforms

6
推荐指数
2
解决办法
6440
查看次数

即使当前实例是派生类,我们如何从基类中的另一个方法调用虚方法?

即使当前实例是派生类,我们如何从基类中的另一个方法调用虚方法?

我知道我们可以调用方法2基地从方法的类派生类使用base.Method2() ,但我想要做的就是从其他的虚拟方法调用它什么基础类.可能吗?

using System;

namespace ConsoleApplication1
{
  class Program
  {
    static void Main( string[] args )
    {
      Base b = new Derived(  );
      b.Method1(  );
    }
  }


  public class Base
  {
    public virtual void Method1()
    {
      Console.WriteLine("Method1 in Base class.");
      this.Method2( );   // I want this line to always call Method2 in Base class, even if the current instance is a Derived object.
      // I want 'this' here to always …
Run Code Online (Sandbox Code Playgroud)

c# virtual-functions base-class derived-class

5
推荐指数
2
解决办法
6734
查看次数

如何将字符串转换为BinaryExpression对象?

假设我们有以下字符串:

string s1 = "a < 5";
string s2 = "b >= 7";
string s3 = "c <= 8";
...
Run Code Online (Sandbox Code Playgroud)

我想将这些字符串转换为类似于我们使用的BinaryExpression对象:

  BinaryExpression b1 = Expression.MakeBinary( ExpressionType.LessThan, Expression.Parameter( typeof( int ), "a" ), Expression.Constant( 5, typeof( int ) ) );
  BinaryExpression b2 = Expression.MakeBinary( ExpressionType.GreaterThanOrEqual, Expression.Parameter( typeof( int ), "b" ), Expression.Constant( 7, typeof( int ) ) );
  BinaryExpression b3 = Expression.MakeBinary( ExpressionType.LessThanOrEqual, Expression.Parameter( typeof( int ), "c" ), Expression.Constant( 8, typeof( int ) ) );
Run Code Online (Sandbox Code Playgroud)

我创建了以下方法:

BinaryExpression ConvertStringToBinaryExpression( string exp …
Run Code Online (Sandbox Code Playgroud)

.net c# expression

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

使用LINQ将List <int>转换为Dictionary <int,int>

说我有以下列表:

  List<int> list = new List<int>() { 5, 5, 6, 6, 6, 7, 7, 7, 7 };
Run Code Online (Sandbox Code Playgroud)

如何将列表转换为字典,其中值是使用LINQ列表中每个不同数字的计数?例如,上面的列表应该转换为包含以下元素的字典:

关键 - >价值

5 - > 2

6 - > 3

7 - > 4

c# linq dictionary list

4
推荐指数
1
解决办法
1603
查看次数

如何捕获使用 MethodInfo.Invoke 调用的方法中抛出的异常?

我有以下代码:

using System;
using System.Reflection;

namespace TestInvoke
{
  class Program
  {
    static void Main( string[] args )
    {
      Method1();
      Console.WriteLine();
      Method2();
    }

    static void Method1()
    {
      try
      {
        MyClass m = new MyClass();
        m.MyMethod( -3 );
      }
      catch ( Exception e )
      {
        Console.WriteLine( e.Message );
      }
    }

    static void Method2()
    {
      try
      {
        string className = "TestInvoke.MyClass";
        string methodName = "MyMethod";
        Assembly assembly = Assembly.GetEntryAssembly();
        object myObject = assembly.CreateInstance( className );
        MethodInfo methodInfo = myObject.GetType().GetMethod( methodName );
        methodInfo.Invoke( myObject, …
Run Code Online (Sandbox Code Playgroud)

c# reflection exception try-catch invoke

3
推荐指数
1
解决办法
2400
查看次数