在C#2.0中,我们可以使用以下值来初始化数组和列表:
int[] a = { 0, 1, 2, 3 };
int[,] b = { { 0, 1 }, { 1, 2 }, { 2, 3 } };
List<int> c = new List<int>(new int[] { 0, 1, 2, 3 });
Run Code Online (Sandbox Code Playgroud)
我想对字典做同样的事情.我知道你可以在C#3.0之后轻松完成这样的事情:
Dictionary<int, int> d = new Dictionary<int, int> { { 0, 1 }, { 1, 2 }, { 2, 3 } };
Run Code Online (Sandbox Code Playgroud)
但它在C#2.0中不起作用.如果没有使用Add或基于现有的集合,是否有任何解决方法?
我想我不明白Type.IsByRef.NET的属性应该表明什么.我认为它应该为引用类型返回true而对值类型返回false,因此与Type.IsValueType属性相反.我不能让它为显然是引用类型的类型返回true.这是一个例子:
using System.Text;
public class Program
{
static void Main(string[] args)
{
try
{
int i = 0;
Console.WriteLine(i.GetType().IsByRef); // returns false - OK
Exception e = new Exception();
Console.WriteLine(e.GetType().IsByRef); // returns false - ??
StringBuilder sb = new StringBuilder();
Console.WriteLine(sb.GetType().IsByRef); // returns false - ??
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
Console.ReadKey(true);
}
}
Run Code Online (Sandbox Code Playgroud)
我在这里错过了什么?