为什么我们不能在同一方法中同时使用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) 为什么在构造函数中抛出异常导致空引用?例如,如果我们运行下面的代码,则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) Capacity属性在List中比在Stack和Queue等其他集合中更有用吗?或者有另一种方法来获得堆栈或队列的容量?
给出以下示例代码:
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)
?
在此示例中,第二个语句生成编译错误,而第一个语句则不生成编译错误.
例如,如果这些代码:
Button button1 = new Button();
// ...
button1.Click -= button1_Clicked;
Run Code Online (Sandbox Code Playgroud)
在执行之前执行:
button1.Click += button1_Clicked;
Run Code Online (Sandbox Code Playgroud)
我没有发现错误或异常,但我想知道这里是否有任何缺点.
如果它是安全的,为什么允许取消订阅从未订阅过的活动?
为什么即使鼠标没有移动,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) 即使当前实例是派生类,我们如何从基类中的另一个方法调用虚方法?
我知道我们可以调用方法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) 假设我们有以下字符串:
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) 说我有以下列表:
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
我有以下代码:
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# ×10
.net ×3
exception ×2
list ×2
base-class ×1
capacity ×1
constructor ×1
delegates ×1
dictionary ×1
events ×1
expression ×1
ienumerable ×1
invoke ×1
linq ×1
mousemove ×1
null ×1
object ×1
picturebox ×1
queue ×1
reflection ×1
return ×1
stack ×1
subscribe ×1
try-catch ×1
unsubscribe ×1
winforms ×1
yield-return ×1