假设您有一个基本Employee类:
class Employee
{
public string Name;
public int Years;
public string Department;
}
Run Code Online (Sandbox Code Playgroud)
然后(在一个单独的类中)我有以下代码片段(我想我理解除了最后一个):
我相信下面的代码片段是有效的,因为数组initiliser创建了一个Employee对象数组,它们与分配给的workforce变量的类型相同.
Employee[] workforceOne = new Employee[] {
new Employee() { Name = "David", Years = 0, Department = "software" },
new Employee() { Name = "Dexter", Years = 3, Department = "software" },
new Employee() { Name = "Paul", Years = 4, Department = "software" } };
Run Code Online (Sandbox Code Playgroud)
然后我有以下代码片段.我相信这是有效的,因为Employee对象数组的含义是实现的Array()类的实现IEnumerable.因此,我相信这就是为什么数组可以分配给IEnumerable?
IEnumerable workforceTwo = new Employee[] {
new Employee() { Name = …Run Code Online (Sandbox Code Playgroud) 有人可以澄清一下C#is关键字.特别是这两个问题:
Q1)第5行; 为什么这会回归真实?
Q2)第7行; 为什么没有施放异常?
public void Test()
{
object intArray = new int[] { -100, -200 };
if (intArray is uint[]) //why does this return true?
{
uint[] uintArray = (uint[])intArray; //why no class cast exception?
for (int x = 0; x < uintArray.Length; x++)
{
Console.Out.WriteLine(uintArray[x]);
}
}
}
Run Code Online (Sandbox Code Playgroud)
MSDN的描述并未澄清情况.它声明is如果满足其中任何一个条件,它将返回true.(http://msdn.microsoft.com/en-us/library/scekt9xw(VS.71).aspx>MDSN文章)
expression is not null. expression can be cast to type.
我不相信你可以对int []进行有效的转换为uint [].因为:
A)此代码无法编译:
int[] signed = new int[] { -100 };
uint[] unsigned …Run Code Online (Sandbox Code Playgroud)