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.
SHARE THIS

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.