Method parameter hides the class member field : Method Parameter « Class « C# / CSharp Tutorial






public class Product {

    public int yearBuilt;
    public double maximumSpeed;

    public int Age(int currentYear) {
        int maximumSpeed = 100;  // hides the field
        System.Console.WriteLine("In Age(): maximumSpeed = " + maximumSpeed);
        int age = currentYear - yearBuilt;
        return age;
    }

    public double Distance(double initialSpeed, double time) {
        System.Console.WriteLine("In Distance(): maximumSpeed = " + maximumSpeed);
        return (initialSpeed + maximumSpeed) / 2 * time;
    }

}


class MainClass{

    public static void Main() {
        Product redPorsche = new Product();
        redPorsche.yearBuilt = 2000;
        redPorsche.maximumSpeed = 150;

        int age = redPorsche.Age(2001);
        System.Console.WriteLine("redPorsche is " + age + " year old.");
        System.Console.WriteLine("redPorsche travels " + redPorsche.Distance(31, .25) + " miles.");

    }
}








7.6.Method Parameter
7.6.1.How Arguments Are Passed
7.6.2.Parameter modifiers
7.6.3.Pass int value to a function
7.6.4.Use a parameter in a member function
7.6.5.A member function with two arguments
7.6.6.Pass int without 'out' and 'ref'
7.6.7.Pass integer by ref
7.6.8.ref parameters
7.6.9.Pass integer value by value
7.6.10.params int[] args
7.6.11.Use out for int type
7.6.12.Creating a method with a reference argument.
7.6.13.Variable arguments to a method in C#.
7.6.14.Method parameter hides the class member field
7.6.15.C# Parameter Modifiers
7.6.16.use of 'outer variable' in anonymous method