C#矛盾而有争议的结构
本文关键字:结构 有争议 矛盾 | 更新日期: 2023-09-27 18:00:52
这篇文章的目的是收集所有整洁而棘手的c#构造和日常不会发生的问题。
因此:
第1期(这是我自己发现的(
屏幕上将打印什么?
class Program
{
private static String Function(out String str)
{
str = "Hello ";
return "world!";
}
private static void Main(string[] args)
{
String x = Function(out x);
Console.WriteLine(x);
}
}
有什么想法吗?:(
第2期是否可以创建匿名类型的泛型集合(如List<>或Dictionary<>(?
var Customer = new { FirstName = "John", LastName = "Doe" };
var customerList = new List<????>();
看看这里的这个:
static List<T> CreateList<T>(params T[] values)
{
return new List<T>(values);
}
static void Main(string[] args)
{
var x = new { A = "Hello", B = "world!" };
var list = CreateList(x);
list.Add(new { A = "Hello again", B = "world!" });
}
分享你的发现。非常感谢。
对于第一个示例,x
的值将是方法的返回值,因为方法调用(包括out参数(发生在返回值赋值之前。
就清单而言,你应该能够做到这一点:
var list = new[]
{
new { a = 1, b = "hi" },
new { a = 1, b = "hi" }
}.ToList();
尽管我不确定编译器是否会确定两个匿名类型相等。