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";
}
}
}
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