0

C#

Why is it possible to create a parameter for a method and a variable in a method with the same name? Shouldn't there be an error that there is already a variable with that name. Or parameor is not a variable

17th Jan 2022, 12:26 PM
â€ŽŰŁÙ†ŰŻŰ±ÙŠÙ‡â€Ž
â€ŽŰŁÙ†ŰŻŰ±ÙŠÙ‡â€Ž - avatar
4 Answers
+ 2
Can you show me such a method?
17th Jan 2022, 1:35 PM
Ipang
+ 2
Main defines <x> = 100, then invokes Num() passing value of <x> as argument. That is the <x> defined in Main(). This is fine but I would rather suggest you to use different names for the argument, to evade such confusion as this. You're welcome to confirm in case of doubt, or where something's not clear.
17th Jan 2022, 3:01 PM
Ipang
+ 1
Look, the Num() method has the "int x" parameter, and the Main() method declares the variable int x = 10; whose value is used as an argument using System; public class Program { public static void Main() { int x = 10; Num(x); } public static void Num(int x) { Console.WriteLine(x); } }
17th Jan 2022, 2:12 PM
â€ŽŰŁÙ†ŰŻŰ±ÙŠÙ‡â€Ž
â€ŽŰŁÙ†ŰŻŰ±ÙŠÙ‡â€Ž - avatar
0
It is possible because they are two different things a far a c# is concerned. Parameters are inextricably linked to method calls and whatever you name them in the method signature does not matter because you are not declaring them as a veriable. It is, of course Ill-advised to name a lot of parameters and veriable, in the same module, all the same thing. Common conventions dispatch with "x" or "y" and suggest more descriptive names and case structure like "taxRate" or "_numOfPeople" under score denoting that it is a private backing field.
21st Jan 2022, 1:14 AM
Kail Galestorm
Kail Galestorm - avatar