Return Value by reference in C# Ref Locals and Ref Return | .NET PLUS

Поделиться
HTML-код
  • Опубликовано: 6 ноя 2024

Комментарии • 5

  • @FullStackAmigo
    @FullStackAmigo  4 года назад

    Do you know about any other places that we can use ref keyword? tell me!

  • @nasirkashefi9166
    @nasirkashefi9166 4 года назад

    very good

  • @eliapetrosino1363
    @eliapetrosino1363 3 года назад

    Hi amigo, so if I rightly understood it's almost the same principle with both value and ref types, changing the stored place (stack / heap) but same use and behaviour of the ref keyword. Maybe because it all happens inside an array which is by its own a reference type? Thank's in advance y buena onda

    • @eliapetrosino1363
      @eliapetrosino1363 3 года назад

      EDIT: is good to account that strings behaviour as a value type by .net devs will, so maybe it's also for this reason. I think if we use ref with a class instance it would gonna change behaviour, am I right? Saluto

    • @FullStackAmigo
      @FullStackAmigo  3 года назад +1

      Hi Elia, First I should mention that strings are reference types just like classes. Second, it doesn't matter that it was an array or not, just focus on what we were working on, which in this example was an element of an array and when we use an index to refer to that element, we receive a copy of the value inside that index of the array not a reference to it.
      In case of array of integers we received a copy of integer and in case of array of strings, we got a copy of the string's reference. but after we used ref keyword, we got a reference to that integer's location in heap, and a reference to the location which stores the reference of string's location! So no matter what, ref returns the address not the value.