This video will show you how the new C# 7 feature of “out variables” can be used to simplify the use of statements that have out variables. The person using one of these statements will no longer have to add an additional line or lines of code to predefine the variables.
- [Instructor] One of the things is always been clunky about C# is the handling of out variables. To demonstrate this we're going to open up a project under exercise files. So I go File, Open, Project Solution and under Exercise Files Chapter One in 01_01, I'll open the Old Cars Solution. If a window like this pops up just close it up, and under Converters go to the Int to String Value Converter. Here on line 17 we have a try parse statement that uses an out value.
Prior to C#7 we needed to declare the return value before the call to try parse as we've done here. That value is then available to use in the rest of the method. Now there's an easier way to do this. So I can just remove the return value line now. With that gone one thing has changed, is if we don't care about the return value at all, we can instead use an underscore. So instead of putting the return value here, we'll change that to an underscore.
And instead of returning the return value for example, maybe I'll just return true. That works great if we don't care about the return value but in this case we do. So instead I'm going to put the return value back in, and look at the first way that we can change it based on C#7. So I can say out int return value. And this defines it directly in line. I can take that return value and use it in the rest of our method.
For example I could take it and down here I could say that the return value equals three. So anytime after I define it, I can use it in the method. One thing I can't do, is I can't take it and move it to before I define it. So I couldn't take it for example, and drop at the top of our method. That will return back an error to us. So let's get rid of that. Since we know the type of the out variable, we can use implicit typing here as well.
So I can change this statement, instead of having it int, to be a var. Now we are no longer explicitly typing it, but implicit typing it instead. Now if I go and hover over the return value, I can see that it's still an integer. We can see that visual studio's reporting back to us that it thinks this is an int, because the out variable attribute is defined as an integer in the try parse method itself. If our out variable were typed as an object, our variable using var would likewise be typed as an object.
Since the type of the out variable's always required on the method decoration, we should always be able to use the var statement here. So there we have it. The new out variable feature. It doesn't change anything earth shattering, but the syntactic sugar make using statements that have out variables a breeze.
- Working with tuples
- Using C# throw expressions
- Inspecting UI structures with Xamarin Inspector
- Identifying memory leaks in iOS and Android
- Recording and playing back tests with Test Record
- Using embedded assemblies in Android