How to pass an object to a method by reference?

0 like 0 dislike
9 views
Good day, recently started to learn the programming language C# and have encountered a small problem passing objects to methods at the link, digging in the literature and the Internet, I found a couple article about the topic and a few sample programs, but with one example so to understand and could not, please help to understand
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 and v2 have the same values"); else Console.WriteLine("v1 and v2 have different values"); v1.Copy(v2); Console.WriteLine("After copying"); if (v1.SameAs(v2)) Console.WriteLine("v1 and v2 have the same values"); else Console.WriteLine("v1 and v2 have different values"); }

What is happening in this part of the code: public bool SameAs(MyClass ob), I understand this method takes as a parameter the class itself ? and what a setting ob ?
Then what is compared ? (if ((ob.alpha == alpha) & (ob.beta == beta));)
And last question, what is it - v1.SameAs(v2) ? If this method call, the fact that it takes as parameter ?
If you do not complicate, shall describe the answer more specific, thanks in advance
by | 9 views

2 Answers

0 like 0 dislike
You have a class MyClass with fields of type int alfa and beta
The class has a method that compares the instance fields (already created object) with another instance of the class MyClass , which is passed as a parameter
by
0 like 0 dislike
1) the Parameters in a method are separated by a comma. ob is the name of your parameter to MyClass.
2) if ((ob.alpha == alpha) & (ob.beta == beta)); the equality Comparison of the class MyClass from another class MyClass. Because this class has only 2 fields, then the comparison here is only two fields alpha and beta.
3)v1.SameAs(v2) is a method call. As parameter it takes a MyClass.
Read more literature about OOP (Object oriented programming).
by

Related questions

0 like 0 dislike
1 answer
110,608 questions
257,186 answers
0 comments
28,740 users