C# -

Parameters

Parameters in C# are used to pass information to methods. This tutorial covers different types of parameters, including value parameters, reference parameters, out parameters, optional parameters, named parameters, and parameter arrays. Understanding these concepts is essential for writing flexible and reusable methods.


1. Value Parameters

Value parameters are the most common type of parameters. They are used to pass a copy of the value to the method. Changes made to the parameter inside the method do not affect the original value.

Example:
        
            namespace ParameterExamples;

class Program
{
    static void Main(string[] args)
    {
        int x = 10;
        Increment(x);
        Console.WriteLine($"Value of x after Increment: {x}"); // Output: 10
    }

    static void Increment(int value)
    {
        value++;
    }
}
        
    

2. Reference Parameters

Reference parameters are used to pass a reference to the actual data. Changes made to the parameter inside the method affect the original data. Reference parameters are declared using the ref keyword.

Example:
        
            namespace ParameterExamples;

class Program
{
    static void Main(string[] args)
    {
        int x = 10;
        Increment(ref x);
        Console.WriteLine($"Value of x after Increment: {x}"); // Output: 11
    }

    static void Increment(ref int value)
    {
        value++;
    }
}
        
    

3. Out Parameters

Out parameters are used to return multiple values from a method. They are similar to reference parameters but are used when the method needs to return a value to the caller. Out parameters are declared using the out keyword.

Example:
        
            namespace ParameterExamples;

class Program
{
    static void Main(string[] args)
    {
        int result;
        bool success = TryParse("123", out result);
        if (success)
        {
            Console.WriteLine($"Parsed number: {result}"); // Output: 123
        }
        else
        {
            Console.WriteLine("Parsing failed.");
        }
    }

    static bool TryParse(string input, out int number)
    {
        return int.TryParse(input, out number);
    }
}
        
    

4. Optional Parameters

Optional parameters allow you to specify default values for parameters. If the caller does not provide a value for an optional parameter, the default value is used.

Example:
        
            namespace ParameterExamples;

class Program
{
    static void Main(string[] args)
    {
        PrintMessage("Hello, World!");
        PrintMessage("Hello, World!", 3);
    }

    static void PrintMessage(string message, int repeatCount = 1)
    {
        for (int i = 0; i < repeatCount; i++)
        {
            Console.WriteLine(message);
        }
    }
}
        
    

5. Named Parameters

Named parameters allow you to specify arguments by the parameter name rather than the parameter position. This can make the code more readable and allows you to omit some optional parameters.

Example:
        
            namespace ParameterExamples;

class Program
{
    static void Main(string[] args)
    {
        DisplayInfo(name: "Alice", age: 30, city: "New York");
        DisplayInfo(age: 25, city: "Los Angeles", name: "Bob");
    }

    static void DisplayInfo(string name, int age, string city)
    {
        Console.WriteLine($"Name: {name}, Age: {age}, City: {city}");
    }
}
        
    

6. Parameter Arrays

Parameter arrays allow you to pass a variable number of arguments to a method. They are declared using the params keyword.

Example:
        
            namespace ParameterExamples;

class Program
{
    static void Main(string[] args)
    {
        int sum1 = AddNumbers(1, 2, 3);
        int sum2 = AddNumbers(4, 5, 6, 7, 8);
        Console.WriteLine($"Sum1: {sum1}");
        Console.WriteLine($"Sum2: {sum2}");
    }

    static int AddNumbers(params int[] numbers)
    {
        int sum = 0;
        foreach (int number in numbers)
        {
            sum += number;
        }
        return sum;
    }
}
        
    

Best Practices


Conclusion

Understanding and using different types of parameters effectively is crucial for writing flexible and reusable methods in C#. This tutorial covered value parameters, reference parameters, out parameters, optional parameters, named parameters, and parameter arrays, along with best practices for using them.