[Newbie Question] – Please help me understand how C# knows to associate variables between methods.

Hello! Sorry again for the complete newbie question. I'm going through a C# course, the beginning few chapters are a lot of programming in C# to work as a calculator. I just got to a lesson that taught how to use multiple methods, and how to pass data between methods. One thing I'm having a lot of trouble grasping is how C# knows where to associate variables from one method to another. I'll post an example code from an assignment I'm working on to create a program that calculates the hypotenuse of a triangle.

using System; public class Hypotenuse { public static void Main() { //Declare variables string side1String, side2String; double result; //Get Side 1 Console.WriteLine("Side 1: "); side1String = Console.ReadLine(); double side1 = Convert.ToDouble(side1String); //Get Side 2 Console.WriteLine("Side 2: "); side2String = Console.ReadLine(); double side2 = Convert.ToDouble(side2String); //Call on CalcHypotenuse, establish its variables and return to result to Main result = CalcHypotenuse(side1, side2); Console.WriteLine("The total is: " +result); } public static double CalcHypotenuse(double firstside, double secondside) { double answer = Math.Sqrt(Math.Pow(firstside, 2) + Math.Pow(secondside, 2)); return answer; } 


The program works, but I don't understand how. Everything up until now needed to be carefully established and linked together, now it seems I can just create a method that has two randomly named doubles, and C# automatically knows to assign them to the Main method's variables for side1 and side2?

by Servatose via /r/csharp

Leave a Reply