请考虑以下陈述:
int? v1 = null;
int? v2 = 5 * v1;
Run Code Online (Sandbox Code Playgroud)
有什么价值 v2?(null或空字符串?)
如何防止编译器将其标记为无效操作?我是否需要遵循自定义异常处理?
有什么用GetHashCode()?我可以使用GetHashCode()?跟踪对象标识吗?如果是这样,你能提供一个例子吗?
我必须将以下int数组复制到Array中:
int[] intArray=new int[] {10,34,67,11};
Run Code Online (Sandbox Code Playgroud)
我试过了
Array copyArray=Array.CreateInstance(typeof(int),intArray.Length,intArray);
intArray.CopyTo(copyArray,0);
Run Code Online (Sandbox Code Playgroud)
但是,似乎我犯了一个错误,所以我没有得到结果.
给出数组时:
int[] a={1,3,4,5,67,8,899,56,12,33}
Run Code Online (Sandbox Code Playgroud)
如果我希望使用LINQ返回偶数
var q=a.where(p=>p%2==0)
Run Code Online (Sandbox Code Playgroud)
如果我使用C#2.0并严格执行func <>委托解决它的方法是什么?
我试过了 :
Func<int, bool> func = delegate(int val) { return val % 2 == 0; };
Run Code Online (Sandbox Code Playgroud)
但我很困惑如何在这里链接数组"a".
假设我有以下代码片段:( 澄清目的/没有很好地形成)
class Employee
{
#region fields
protected string _empID;
protected string _empName;
protected readonly string _ssn;
#endregion
public Employee(){}
public Employee(string _empID,string _empName,string _ssn)
{
this._empID =_empID;
this._empName=_empName;
this._ssn = _ssn;
}
}
class Manager : Employee
{
private string _branchID;
public Manager(int _branchID):base(string _empID,string _empName,string _ssn)
{
this._branchID=_branchID;
}
}
static void Main()
{
Manager mgr = new Manager("1","sam","xxx","Branch1");
}
Run Code Online (Sandbox Code Playgroud)
使用base 关键字我正在调用父类构造函数.
在这种情况下,如何组织继承?我有一些不好的假设如下:
由于Manager是从Employee派生的,因此Manager类被填充(empID,empName,ssn)
-----------------
Manager
-----------------
empID
empName
ssn
branchID
Run Code Online (Sandbox Code Playgroud)
第1步:构造函数调用:base("1","sam","xxx")
第2步:基类(Employee)构造函数填充派生类字段(empID,empName,ssn)
步骤3:branchID由派生类构造函数指定
....... …
我是C#的新手.最近我读了一篇文章.建议
"接口的一个实际用途是,当创建一个可以的接口引用时
处理实现该接口的不同类型的对象."
基于我测试的(我不确定我的理解是否正确)
namespace InterfaceExample
{
public interface IRide
{
void Ride();
}
abstract class Animal
{
private string _classification;
public string Classification
{
set { _classification = value;}
get { return _classification;}
}
public Animal(){}
public Animal(string _classification)
{
this._classification = _classification;
}
}
class Elephant:Animal,IRide
{
public Elephant(){}
public Elephant(string _majorClass):base(_majorClass)
{
}
public void Ride()
{
Console.WriteLine("Elephant can ride 34KPM");
}
}
class Horse:Animal,IRide
{
public Horse(){}
public Horse(string _majorClass):base(_majorClass)
{
}
public void Ride()
{ …Run Code Online (Sandbox Code Playgroud) 因为我可以使用委托执行异步操作,我怀疑在我的应用程序中使用System.Threading的机会很小.是否有任何基本情况我无法避免System.Threading?
(只是我在学习阶段).
示例:
class Program
{
public delegate int Total (int a,int b);
static void Main()
{
Total tl = new Total(SumUp);
IAsyncResult isany=tl.BeginInvoke(20,20, null, null);
while (!isany.IsCompleted)
{
Console.WriteLine("Busy with other work");
}
int ans = tl.EndInvoke(isany);
Console.WriteLine("Result ={0}", ans);
}
static int SumUp(int a,int b)
{
a = a * a;
b = b * b;
return a + b;
}
}
Run Code Online (Sandbox Code Playgroud) 是
public event Action delt = () => { Console.WriteLine("Information"); };
Run Code Online (Sandbox Code Playgroud)
一个重载版本
Action<int, int> delg = (a, b) => { Console.WriteLine( a + b); }; ?
Run Code Online (Sandbox Code Playgroud)
我的意思是Action <> delegate是"事件动作"的重载版本?