-2

There is a Statement that:

Methods are called by objects and not by reference.

But I don't understand, what is the Difference between reference and Object.

Consider the Example:

Animal a=new Animal();

(or)

Base b=new Derived();
bolov
  • 72,283
  • 15
  • 145
  • 224
nuli bunny
  • 17
  • 5

1 Answers1

1

I can explain like below

This is an object

public class Person
{

    public string Name { get; set; }
    public string Surname { get; set; }

    public String SayHello()
    {
        return "Hello";
    }

}

You cannot access directly Name, Surname and SayHello from Object like below,

Person.Name = "Onur";
Person.Surname = "Tekir";
Person.SayHello();

You should create instanca of an object. This instance is a reference of object

Person person = new Person();

Now, you can access properties and methods of reference of Person object,

person.Name = "Onur";
person.Surname = "Tekir";
person.SayHello();
Onur Tekir
  • 220
  • 1
  • 3