Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

how to return multiple c-sharp types using single Method in .net

1). If we want to return Multiple types from using single method without casting.
C# provides beautiful feature "Dynamic" keyword.

See below example.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PocProj
{
    class Program
    {
        static void Main(string[] args)
        {

            int x = Method("int");
            string word = Method("string");
            bool isTrue = Method("bool");
        }

        public static dynamic Method(string key)
        {
            switch (key)
            {
                case "bool":
                    return true;
                    break;

                case "int":
                            return 1;
                    break;

                case "string":
                            return "Hi";
                    break;
            }

            return "NULL";
        }
    }
}

Screen Shot.




This post first appeared on Dot Net Tutorial For Beginners With Examples, please read the originial post: here

Share the post

how to return multiple c-sharp types using single Method in .net

×

Subscribe to Dot Net Tutorial For Beginners With Examples

Get updates delivered right to your inbox!

Thank you for your subscription

×