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

  Рет қаралды 3,624

tutorialsEU - C#

tutorialsEU - C#

Күн бұрын

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
kzbin.info...
C# Channel (This one right here!)
kzbin.info...
Android Channel!
kzbin.info...
And our Unity Channel!
kzbin.info...
Here are the links by the way!
To become a great C# Developer check out our C# Learning Path: bit.ly/2TYku2a
FREE C# Download 9 MUST have .NET Developer tools: 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: goo.gl/rBFh3x
Android: / @tutorialseuandroid
C#: / @tutorialseuc
Unity: / @tutorialseuunity
Facebook: / tutorialseu-1093802040...
LinkedIn: / tutorialseu
Discord: / discord

Пікірлер: 8
@tutorialsEUC
@tutorialsEUC 2 жыл бұрын
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! kzbin.info/aero/PLSr9CPTtmP9g6MFLk3KNY-EbWWQjKMEpd Make sure to subscribe to all of our channels! You don´t want to miss our uploads! Main Channel kzbin.info C# Channel (This one right here!) kzbin.info Android Channel! kzbin.info And our Unity Channel! kzbin.info Here are the links by the way! To become a great C# Developer check out our C# Learning Path: bit.ly/2TYku2a FREE C# Download 9 MUST have .NET Developer tools: page.tutorials.eu/tools
@sefatergbashi
@sefatergbashi Жыл бұрын
This is so well-explained. Amazing. Thank you very much!
@johnconnor9787
@johnconnor9787 Ай бұрын
Can we say that "in" keyword makes the variable to be treated as a constant within that function?
@Polynuttery
@Polynuttery 2 жыл бұрын
You forgot the out bit of the video.
@maxfun6797
@maxfun6797 2 жыл бұрын
He covers out at 15:25
@richardmeyer418
@richardmeyer418 2 жыл бұрын
@@maxfun6797 To be fair, it did sound like the video ended at about 15:15 - but it does carron as you pointed out.
@johnconnor9787
@johnconnor9787 Ай бұрын
3:22 Long explanation and nothing happens on the screen. This way the viewer will lose the point of the story
@weneedlittlepatience
@weneedlittlepatience 29 күн бұрын
dislike
IS THIS REAL FOOD OR NOT?🤔 PIKACHU AND SONIC CONFUSE THE CAT! 😺🍫
00:41
New Gadgets! Bycycle 4.0 🚲 #shorts
00:14
BongBee Family
Рет қаралды 16 МЛН
Java is ALWAYS Pass By Value. Here's Why
5:22
Coding with John
Рет қаралды 116 М.
Value & Reference types in C#. Write Better Code!
15:09
Tarodev
Рет қаралды 32 М.
What does the ref keyword actually do in C#?
13:59
Nick Chapsas
Рет қаралды 27 М.
What is the Difference Between a Pointer and a Reference C++
7:58
Paul Programming
Рет қаралды 421 М.
C# Yield Return: What is it and how does it work?
15:09
Brian Lagunas
Рет қаралды 54 М.
What is Span in C# and why you should be using it
15:15
Nick Chapsas
Рет қаралды 247 М.
Passing by value and reference - C# Gotcha! Interview Questions.
13:33
.NET Boxing (And Unboxing)
15:19
Coding Tutorials
Рет қаралды 1,5 М.
C#: The Heap and the Stack
9:50
Edward Tanguay
Рет қаралды 36 М.
IS THIS REAL FOOD OR NOT?🤔 PIKACHU AND SONIC CONFUSE THE CAT! 😺🍫
00:41