N
N
Nikita072018-08-20 10:59:17
OOP
Nikita07, 2018-08-20 10:59:17

How to pass an object to a method by reference?

Good afternoon, I recently started to study the C # programming language and ran into a small problem of passing objects to methods by reference, rummaging through the literature and the Internet, I found a couple of articles on this topic and several program examples, but I couldn’t figure it out with one example, please help me figure it out

using System;
class MyClass
{
    int alpha, beta;
    public MyClass(int A, int B)
    {
        alpha = A;
        beta = B;
    }
    public bool SameAs(MyClass ob)
    {
    if ((ob.alpha == alpha) & (ob.beta == beta));
      return true;
    else
      return false;
    }
    public void Copy(MyClass ob)
    {
      alpha = ob.alpha;
      beta=ob.beta;
    }
    public void Show()
    {
    Console.WriteLine("alpha: {0}, beta {1}", alpha, beta);
}

class Program
{
   static void Main()
   {
     MyClass v1 = new MyClass (4,5);
     MyClass v2 = new MyClass (6,7);
     Console.Write("v1: ");
     v1.Show();
     Console.Write("v2: ");
     v2.Show();

     if (v1.SameAs(v2))
       Console.WriteLine("v1 и v2 имеют одинаковые значения");
    else
       Console.WriteLine("v1 и v2 имеют разные значения");

    v1.Copy(v2);

    Console.WriteLine("После копирования");
     if (v1.SameAs(v2))
       Console.WriteLine("v1 и v2 имеют одинаковые значения");
    else
       Console.WriteLine("v1 и v2 имеют разные значения");
}

What happens in this part of the code: public bool SameAs(MyClass ob), as I understand it, this method takes the class itself as a parameter? and what is the ob parameter?
What is then compared? (if ((ob.alpha == alpha) & (ob.beta == beta));)
And the last question, what is v1.SameAs(v2) ? If it's a method call, what does it take as a parameter?
Please be more specific if you don't mind, thanks in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
ever Clever, 2018-08-20
@Nikita07

You have a class MyClass with fields of type int alfa and beta
The class has a method that compares the fields of an instance (already created object) with another instance of the class MyClass , which is passed as a parameter

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question