LEARN how to pass VALUE TYPES by REFERENCE in C# - Ref, In and Out

Описание к видео LEARN how to pass VALUE TYPES by REFERENCE in C# - Ref, In and Out

If you've ever tried passing a value type to a function and had it fail with the error "Cannot pass by reference", then this video is for you. Want more?! Get more over here!    • C# 2021 Tutorials  

Make sure to subscribe to all of our channels! You don´t want to miss our uploads!

Main Channel
https://www.youtube.com/c/tutorialsEU...
C# Channel (This one right here!)
https://www.youtube.com/c/tutorialsEU...
Android Channel!
https://www.youtube.com/c/tutorialsEU...
And our Unity Channel!
https://www.youtube.com/c/tutorialsEU...

Here are the links by the way!

To become a great C# Developer check out our C# Learning Path: https://bit.ly/2TYku2a

FREE C# Download 9 MUST have .NET Developer tools: https://page.tutorials.eu/tools

The C# language allows developers to call functions that take parameters by reference, but not all types are allowed. In this article, we'll explore when it's okay to use ref or out keyword for your parameter types.

In this video, we will talk about passing values by reference.

Let's say we want to swap the values of two numbers. In programming swapping is easy.

Imagine you have a coffee cup that has orange juice in it and a juice glass that has coffee in it.

And you want to fix that by swapping the content of these cups.

Simply follow these steps.

1. Introduce a new cup which we will need temporarily.
2. Pour the coffe in the juice glass into the third empty cup.
3. Now that the juice glass is empty we can pour the orange juice in the coffee cup into the juice glass.
4. And finally since the coffee cup is now empty we can pour the coffee in the third temp cup back into the coffee cup.

In programming swapping two numbers is the same as the method above

The out keyword is used when the value we passed must be changed by the method.

Now it is important to note that the value of num before the TryParse does not matter.

Because even if we set the value of num to some value, the TryParse method will change it anyway.

If the parsing was successful then num will be the parsed value otherwise it will have it's default value which is Zero.

To understand the mechanics of the out keyword in more depth let's try to write our own method that takes an out number.

We all know that we can't divide by zero, so let's write a method called TryDivide that will try to divide number 1 by number 2 and return the results. But similar to the TryParse method our TryDivide method will also return a bool indicating whether the division was successful or not

So using the out keyword is a good idea when we want to get a result out of a method that will TryXYZ (try to do something), and at the same time return a bool that we can check to see if it was successful or not.

So basically what you need to understand is that while ref and out are treated differently at compile time. They both do the same thing at runtime.

Also passing values by reference is better performance-wise but not a degree where you could notice it. You will be more concerned about the difference between using ref and out vs call by value when you are working on highly performant systems or real-time systems.

Finally the In keyword.

Let's assume you are working on a very busy money transaction system, where every byte you save in memory can mean a lot. also writing secure code is very crucial!.

In this case, you want to pass the balance of the user by reference to avoid copying the value, and at the same time, you are afraid that the called method might change the balance by mistake.

This is where "In" truly shines !.

Values sent using the In keyword will allow us to send the variable by reference while protecting it from being modified at the same time.

#csharp #programming #tutorial #visualstudio #learn #elearning

tutorialsEU offers you free video tutorials about programming and development for complete beginners up to experienced programmers.
This includes C#, Unity, Python, Android, Kotlin, Machine Learning, etc.
Stay tuned and subscribe to tutorialsEU: https://goo.gl/rBFh3x

Android:    / @tutorialseuandroid  
C#:    / @tutorialseuc  
Unity:    / @tutorialseuunity  

Facebook:   / tutorialseu-109380204093233  

LinkedIn:   / tutorialseu  

Discord:   / discord  

Комментарии

Информация по комментариям в разработке