Answer the question
In order to leave comments, you need to log in
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 имеют разные значения");
}
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question