- Mastering Visual Studio 2017
- Kunal Chowdhury
- 287字
- 2021-07-15 17:26:39
New changes with the out variables
Currently in C#, we need to first declare a variable before we pass it as an out parameter to a method. You can use a var while declaration if you initialize them in the same line, but when you don't want to initialize explicitly, you must declare them, specifying the full type:
// predeclaration of 'out' variable was mandatory int result; // or, var result = 0; string value = "125"; int.TryParse(value, out result); Console.WriteLine("The result is: " + result);
In C# 7.0, the out variables can be declared right at the point where they are passed as an out parameter to a method. You can now directly write int.TryParse(value, out int result); and get the value of the out parameter, to use it in the scope of the enclosing block:
static void Main(string[] args) { string value = "125"; int.TryParse(value, out int result); Console.WriteLine("Result: " + result); }
You can also use var instead of a strong type declaration, like int.TryParse(value, out var result);, and the compiler will define it properly:
static void Main(string[] args) { string value = "125"; int.TryParse(value, out var result); Console.WriteLine("Result: " + result); }
If you have already written code in the existing way of declaration before passing the out parameter to a method, Visual Studio 2017 will allow you to leverage this new feature of C# 7.0 from the light bulb icon:
From the popup, you can fix all the similar occurrences, in the current document, the entire project, or at the solution level, by clicking the links highlighted as shown in the preceding screenshot.
- PWA入門(mén)與實(shí)踐
- Cocos2d-x游戲開(kāi)發(fā):手把手教你Lua語(yǔ)言的編程方法
- PostgreSQL 11從入門(mén)到精通(視頻教學(xué)版)
- Java設(shè)計(jì)模式及實(shí)踐
- PhpStorm Cookbook
- C語(yǔ)言程序設(shè)計(jì)同步訓(xùn)練與上機(jī)指導(dǎo)(第三版)
- 數(shù)據(jù)結(jié)構(gòu)案例教程(C/C++版)
- jQuery炫酷應(yīng)用實(shí)例集錦
- Python機(jī)器學(xué)習(xí):預(yù)測(cè)分析核心算法
- Python3.5從零開(kāi)始學(xué)
- Getting Started with Nano Server
- Scratch從入門(mén)到精通
- 虛擬現(xiàn)實(shí):引領(lǐng)未來(lái)的人機(jī)交互革命
- Python程序設(shè)計(jì)教程
- Scratch少兒編程高手的7個(gè)好習(xí)慣