In Javascript I can make a function that returns an object:
function person() {
return { name: "John Doe", age: 20, isMarried: false };
}
console.log("Name: "+ person().name +". Age: "+ person().age +". Is married: "+ person().isMarried);
OUTPUT:
> "Name: John Doe. Age: 20. Is Married: false"
I was wondering if it's possible to do something like this in C#? I've been reading about delegates, dictionaries and anonymous methods but I still have no clue about this.
In Javascript I can make a function that returns an object:
function person() {
return { name: "John Doe", age: 20, isMarried: false };
}
console.log("Name: "+ person().name +". Age: "+ person().age +". Is married: "+ person().isMarried);
OUTPUT:
> "Name: John Doe. Age: 20. Is Married: false"
I was wondering if it's possible to do something like this in C#? I've been reading about delegates, dictionaries and anonymous methods but I still have no clue about this.
Share Improve this question edited Nov 30, 2013 at 18:44 deathismyfriend 2,1922 gold badges19 silver badges25 bronze badges asked Nov 30, 2013 at 18:07 fnxfnx 3931 gold badge5 silver badges19 bronze badges 4- Sure just instantiate a class – zzzzBov Commented Nov 30, 2013 at 18:11
- Returning a Dictionary<string, object> would be similar. – psquared Commented Nov 30, 2013 at 18:13
-
Anonymous Types
is what youre looking for. JLe's second snippet uses this but you should look upanonymous types
if you want to investigate further. – Jeff Commented Nov 30, 2013 at 18:15 - Wow, so many great answers! However I use C# in Unity and apparently it doesn't support System.Dynamic or System.Tuple either. I think I'm left with Hessam's answer using Out but I don't know how I can return multiple values with it. Should I edit my question to be more specific or should I head to Unity answers site and ask there? – fnx Commented Nov 30, 2013 at 18:33
6 Answers
Reset to default 8static void Main(string[] args)
{
dynamic p = person();
Console.WriteLine("Name: {0}, Age: {1}, Is married: {2}", p.name, p.age, p.isMarried);
}
static dynamic person()
{
return new { name = "John Doe", age = 20, isMarried = false };
}
More info about the dynamic
keyword
You can return an object
:
object Person() {
var p = new ExpandoObject();
p.Name = "John Doe";
p.Age = 20;
p.Married = false;
return p;
}
You can create is even more "dynamic" as well:
object Person() {
return new {
Name = "John Doe",
Age = 20,
Married = false
};
}
You can use this
public void method(out string Name, out int Age, out bool Married){
//body of method
}
in .NET 4.5 there is something called as TUPLE. You can use that. For more info.. You can follow the link.
http://msdn.microsoft./en-us/library/system.tuple(v=vs.110).aspx
You can also return a tuple which holds multiple values.
static void Main(string[] args)
{
var tuple = GetItem()
Console.WriteLine(string.Format("Item1: {0}", tuple.Item1));
Console.WriteLine(string.Format("Item2: {0}", tuple.Item2));
}
public static Tuple<string, int> GetItem()
{
return new Tuple<string, int>("Some item from database.", 4);
}
class Program
{
static void Main(string[] args)
{
Tuple<int, string, double, string> emp = GetEmployeeInfo();
Console.WriteLine("Emplooyee Id :: " + emp.Item1);
Console.WriteLine("Employee Name :: " + emp.Item2);
Console.WriteLine("Employee Salary :: " + emp.Item3);
Console.WriteLine("Employee Address :: " + emp.Item4);
}
static Tuple<int, string, double, string> GetEmployeeInfo()
{
Tuple<int, string, double, string> employee;
employee = new Tuple<int, string, double, string>(1001, "Uthaiah Bollera", 4500000.677, "Bangalore Karnataka!");
return employee;
}
}