C# 7.0 新特性与 WPF 应用开发入门
1. C# 7.0 新特性
1.1 ref 返回和局部变量
自 C# 1.0 起,语言就支持使用ref关键字按引用将参数传递给方法,但没有机制能安全返回栈或堆内存位置的引用。在 C# 7.0 中,开发者可以按引用返回值并将其存储在局部变量中作为引用指针。
在看按引用返回的示例前,先看按值返回与按引用传递参数结合的例子:
public static void DemoReturnAsValue() { var count = 0; var index = 0; string[] names = { "Kunal", "Manika", "Dwijen" }; string name = GetAsValue(names, index, ref count); Console.WriteLine("No. of strings in the array: " + count); Console.WriteLine("Name at {0}th index is: {1}\n", index, name); name = "Rajat"; Console.WriteLine("The value of 'name' variable changed to:" + name); Console.WriteLine("The new name at {0}th index is still: {1}\n", index