0

C# 'this' using help pls

I have no idea what happens there, if could you explain me what is this code doing step by step it would be nice. Input give us 2 values type of double, and i think this line: Avg avg = new Avg(num1, num2); is meaning that we pass num1 and num2 to the Avg class by constructor and 'this.' is assigning them to the declared variables inside class ? class Program { static void Main(string[] args) { double num1 = Convert.ToDouble(Console.ReadLine()); double num2 = Convert.ToDouble(Console.ReadLine()); Avg avg = new Avg(num1, num2); Console.WriteLine(avg.GetAvg()); } } class Avg { double num1; double num2; //create the constructor public Avg(double num1,double num2 ) { this.num1 = num1; this.num2 = num2; } public double GetAvg() { return (num1 + num2)/2; } } }

2nd May 2021, 11:50 AM
Mateusz
Mateusz - avatar
1 Antwort
+ 3
Here you see basics of objects oriented programming oop. With new will be an object created / initiated. After that called its method for getting the average of two input nunbers.
2nd May 2021, 1:27 PM
JaScript
JaScript - avatar