Top Ad unit 728 × 90

random

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.


how to return multiple c-sharp types using single Method in .net Reviewed by Vikas Kumar Singh on December 11, 2017 Rating: 5

No comments:

All Rights Reserved by DotNetBasic.com © 2019

Contact Form

Name

Email *

Message *

© 2018 All rights reserved by DotNetBasic. Powered by Blogger.