A Comprehensive Resource for Microsoft Technologies

Welcome, your go-to destination for everything related to .NET and Microsoft technologies. With over 10 years of experience in the IT industry, I am excited to share my knowledge and insights on this platform. This blog is dedicated to providing valuable information, tips, and tutorials that are not only relevant to the rapidly evolving IT industry but also essential for individuals working on real-time projects and preparing for interviews

C# Tutorial 6.2: Parameters and return types

 In C#, a method is a block of code that performs a specific task, and parameters and return types are crucial components of method signatures.

Parameters:

1. Defining a Method with Parameters:

In C#, you can define a method with parameters to receive input values.

class Program
{
    // Method with parameters
    static void PrintMessage(string message)
    {
        Console.WriteLine(message);
    }

    static void Main()
    {
        // Calling the method with an argument
        PrintMessage("Hello, C#!");
    }
}

In this example, PrintMessage is a method that takes a string parameter named message.

2. Multiple Parameters:

You can have multiple parameters in a method.

class Program
{
    // Method with multiple parameters
    static void PrintDetails(string name, int age)
    {
        Console.WriteLine($"Name: {name}, Age: {age}");
    }

    static void Main()
    {
        // Calling the method with multiple arguments
        PrintDetails("John Doe", 25);
    }
}

3. Default Values:

You can provide default values for parameters.

class Program
{
    // Method with a default parameter value
    static void PrintGreeting(string name, string greeting = "Hello")
    {
        Console.WriteLine($"{greeting}, {name}!");
    }

    static void Main()
    {
        // Calling the method with and without providing the second argument
        PrintGreeting("Alice");
        PrintGreeting("Bob", "Hi");
    }
}

Return Types:

1. Methods with Return Types:

A method can return a value. Specify the return type in the method signature.

class Program
{
    // Method with a return type
    static int Add(int a, int b)
    {
        return a + b;
    }

    static void Main()
    {
        // Calling the method and storing the result
        int sum = Add(3, 5);
        Console.WriteLine($"Sum: {sum}");
    }
}

2. Void Return Type:

If a method doesn't return any value, use the void return type.

class Program
{
    // Method with void return type
    static void DisplayMessage(string message)
    {
        Console.WriteLine(message);
    }

    static void Main()
    {
        // Calling the method with no need to store the result
        DisplayMessage("Welcome to C#!");
    }
}

3. Returning Objects:

A method can also return objects of custom classes or built-in types.

class Program
{
    // Method returning an object
    static Person CreatePerson(string name, int age)
    {
        return new Person { Name = name, Age = age };
    }

    static void Main()
    {
        // Calling the method and working with the returned object
        Person person = CreatePerson("Jane Doe", 30);
        Console.WriteLine($"Person: {person.Name}, Age: {person.Age}");
    }
}

// Custom class for demonstration
class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}

Now you have a basic understanding of how to work with parameters and return types in C#. Feel free to experiment and build upon these concepts in your own projects!