Ads

C# Programming 1 - How to return multiple values from method?

Hello All,

Welcome to C# Programming Language, I am Chakrapani Upadhyaya a full Stack Engineer.

In this article we are going to learn How to Return Multiple values from method to caller.



Scenario:

Consider you have a scenario that you need two return value from function, means function should return discount price based on location i.e. location and discount will be the output, we can achieve this easily with dictionary or List but we do not want that. 

In previous versions of C# we can achieve this by Tuples, example shown below


A tuple is a data structure that has a specific number and sequence of values, the values in a tuple can either be of the same type or even be of dissimilar

 

public class Program
{
    public static void Main(string[] args)
    {
        //Getting the value now
        var discount = Program.GetLocationDiscount();

        Console.WriteLine($"Location : {discount.Item1} and 
                            its discount is :{discount.Item2}");

        Console.Read();
    }

    public static Tuple<string, int> GetLocationDiscount()
    {
        //Finding Location and its discount logic goes here
        //But as of now we use hardcoded values
        return Tuple.Create("India", 30);
    }

}


And output will be




Above example we have used tuples to return multiple values and in caller we are getting Tuples with two values have Item1 and Item2 as properties.

But in latest version C# Syntax have been changed, now its super easy, Tuple keyword is not required.

Example shown below...


public class Program
{
    public static void Main(string[] args)
    {
        //Getting the value now
        var discount = Program.GetLocationDiscount();

        Console.WriteLine($"Location : {discount.Item1} and its 
                            discount is :{discount.Item2}");

        Console.Read();
    }

    public static (string, int) GetLocationDiscount()
    {
        //Finding Location and its discount logic goes here
        //But as of now we use hardcoded values
        return ("India", 30);
    }
}


You can also provide names to your elements (so they are not "Item1", "Item2" etc). You can do it by adding a name to the signature or the return methods:


public class Program
{
    public static void Main(string[] args)
    {
        //Getting the value now
        var discount = Program.GetLocationDiscount();

        Console.WriteLine($"Location : {discount.location} and its 
                                       discount is :{discount.discount}");

        Console.Read();
    }

    public static (string location, int discount) GetLocationDiscount()
    {
        //Finding Location and its discount logic goes here
        //But as of now we use hardcoded values
        return ("India", 30);
    }
}


Or You can also do like below.


public class Program
{
    public static void Main(string[] args)
    {
        //Getting the value now
        (string location, int discount) = Program.GetLocationDiscount();

        Console.WriteLine($"Location : {location} and its discount is :discount}");

        Console.Read();
    }


    public static (string, int) GetLocationDiscount()
    {
        //Finding Location and its discount logic goes here
        //But as of now we use hardcoded values
        return (location: "India", discount: 30);
    }
}


That's it for this article, please let us know in case any concerns.

Until then take care by see you in next article.



Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !