2

ok, so in javascript, we can declare an object like this,

var obj={name:"Irshu",age:22};
console.log(obj);

How do we do the same in c#? the reason i ask because my function need to return a string and a bool together. I dont want to create a class for it, and i dont want to use the dictionary. Are there any alternatives?

public void Message(){
var obj=GetObject(val);
Messagebox.Show(Convert.ToString(obj.ind));
}

public object GetObject(string val){

 return new {ind=val,flag=true};
}

This is not valid, is it?

2
  • You can either use Tuple<string, int> (although names for accessors are ugly and lack descriptive power) or dynamic with ExpandoObject (and throw away compile-time safety). Commented Jan 21, 2014 at 8:27
  • 4
    I think the most powerful advice here is to code in the style of the language you're working with, not the language you're familiar with. Commented Jan 21, 2014 at 8:33

3 Answers 3

3

.Net supports ExpandoObject since .NET 4.

http://msdn.microsoft.com/en-us/library/system.dynamic.expandoobject%28v=vs.110%29.aspx

It lets you declare the object and add properties as your would in javascript.

Traditionally it is for JS interop and I can't recommend it for production work. Tuple<T> is more appropriate as you get strong typing for free. Ultimately you will write less code and see less runtime errors.

What you have in your code is an anonymous type. Anonymous types cannot exist outside the scope in which they are declared. Generally, we use these for transforming LINQ results to temporary objects.

Sign up to request clarification or add additional context in comments.

Comments

1

You can't return anonymous types from a method. You can do however something like this:

public void Message(){
    var obj = new { ind = "oaiwejf", flag = true };
    Messagebox.Show(obj.ind);
}

EDIT

Check this MSDN article

8 Comments

Well you can return instances of anonymous types. It's just that the calling code won't know (at compile-time) what the properties are. You can still return it as object, or even dynamic
obj.ind would be considered a string so the Convert is unnecessary.
Well, I guess you could return it as an object, although I never really tried that. (Argh, Jon beat me :D)
@siva.k That's right. Stupid copy paste. Thanks for your comment.
@SOReader, there's no boxing involved here; anonymous types are reference types. And yes, it is the object itself that is returned (well, a reference to it, actually), it's just that the caller won't know its actual type statically.
|
0

turns out, its posible, one genius on the internet posted this:

public void Message()
{
   var obj=GetObject("Irshu");
   var y=  Cast(obj, new { ind= "", flag= true });
   Messagebox.Show(y.ind); //alerts Irshu
}

public object GetObject(string val){

 return new {ind=val,flag=true};
}

T Cast<T>(object obj, T type)
{
  return (T)obj;
}

2 Comments

Not sure I would call that "genius"... it's really, really bad practice IMO, because you rely on your own knowledge of what GetObject returns, which is not enforced by anything. If you change the return value of GetObject, and forget to update Message, the compiler won't warn you. Also, it only works within the same assembly.
well...the reason because i dont want to declare a class just for a one time use. I dont seems to have other alternatives other than assigning to public properties, GetObject is inside another class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.