[Solved] Difference between void SomeMethod(ref Object obj) and void SomeMethod(Object obj) [duplicate]


When you don’t pass an object with ref keyword then object reference is passed by value. Whereas, in other case object is passed by reference. You can get better explanation with following example.

Example:

private void button1_Click_2(object sender, EventArgs e)
        {

        Student s = new Student
            {
                FirstName = "Svetlana",
                LastName = "Omelchenko",
                Password = "hh",
                modules = new string[] { "001", "002", "003", "004" }
            };
        SomeMethod(s);

        Console.WriteLine(s.FirstName); //will output Svetlana

    }


    private void SomeMethod(Student s)
    {
        s = new Student();
        s.FirstName = "New instance";
    }
    class Student
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Password { get; set; }
        public string[] modules { get; set; }
    }

now if you have that method like this

private void SomeMethod(ref Student s)
        {
            s = new Student();
            s.FirstName = "New instance";
        }

then output will be New instance

solved Difference between void SomeMethod(ref Object obj) and void SomeMethod(Object obj) [duplicate]