Rajanikant Hawaldar
I will pass class obj with "ref" through an argument to method and I will change properties inside of that method. Now what will happen to object outside that method?
By Rajanikant Hawaldar in C# on Dec 18 2020
  • Kiran Mohanty
    Jan, 2021 4

    If we define any input parameter as “ref”, we basically mark it as reference variable. Hence changing value of it inside child method will reflect change in parent method as well.

    • 1
  • Shekhar Kumar
    Jan, 2021 1

    namespace Learn
    {
    public class Demo
    {
    public int Id { get; set; }
    }
    public class Consume
    {
    Demo d1 = new Demo();
    public void Change(ref Demo d1)
    {
    d1.Id = 3;
    Console.WriteLine(d1.Id);
    }
    }
    class Program
    {
    static void Main(string[] args)
    {
    Demo d1 = new Demo();
    d1.Id = 1;
    Consume c1 = new Consume();
    c1.Change(ref d1);
    Console.WriteLine(d1.Id);
    }
    }
    }

    If this is the program we need. Then object property will retain its value outside the method.

    • 1


Most Popular Job Functions


MOST LIKED QUESTIONS