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

แชร์
ฝัง
  • เผยแพร่เมื่อ 27 ก.ค. 2024
  • 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
    th-cam.com/users/tutorialsEU...
    C# Channel (This one right here!)
    th-cam.com/users/tutorialsEU...
    Android Channel!
    th-cam.com/users/tutorialsEU...
    And our Unity Channel!
    th-cam.com/users/tutorialsEU...
    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

ความคิดเห็น • 9

  • @tutorialsEUC
    @tutorialsEUC  2 ปีที่แล้ว +3

    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! th-cam.com/play/PLSr9CPTtmP9g6MFLk3KNY-EbWWQjKMEpd.html
    Make sure to subscribe to all of our channels! You don´t want to miss our uploads!
    Main Channel
    th-cam.com/users/tutorialsEU
    C# Channel (This one right here!)
    th-cam.com/users/tutorialsEUC
    Android Channel!
    th-cam.com/users/tutorialsEUAndroid
    And our Unity Channel!
    th-cam.com/users/tutorialsEUUNITY
    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

  • @br0k_ger79
    @br0k_ger79 2 วันที่ผ่านมา

    Awsome it realy helpt me out thank you!!!!!!!

  • @sefatergbashi
    @sefatergbashi ปีที่แล้ว +1

    This is so well-explained.
    Amazing.
    Thank you very much!

  • @johnconnor9787
    @johnconnor9787 3 หลายเดือนก่อน

    Can we say that "in" keyword makes the variable to be treated as a constant within that function?

  • @Polynuttery
    @Polynuttery 2 ปีที่แล้ว +2

    You forgot the out bit of the video.

    • @maxfun6797
      @maxfun6797 2 ปีที่แล้ว +2

      He covers out at 15:25

    • @richardmeyer418
      @richardmeyer418 2 ปีที่แล้ว +1

      @@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 หลายเดือนก่อน

    3:22 Long explanation and nothing happens on the screen. This way the viewer will lose the point of the story

  • @weneedlittlepatience
    @weneedlittlepatience 2 หลายเดือนก่อน

    dislike