29

I'm working with Xamarin, and I need something that looks like this:

public Colors = new object() {
  Blue = Xamaring.Color.FromHex("FFFFFF"),
  Red = Xamarin.Color.FromHex("F0F0F0")
}

So I can later do something like this:

myObject.Colors.Blue // returns a Xamarin.Color object

But of course, this doesn't compile. Aparently, I need to create a complete new class for this, something I really don't want to do and don't think I should. In javascript, I could do something like this with:

this.colors = { blue: Xamarin.Color.FromHex("..."), red: Xamarin... }

Is there a C sharp thing that can help me achieve this quickly? Thank you

2
  • 1
    Well you could create a dynamic object (msdn.microsoft.com/en-us/library/…, msdn.microsoft.com/en-us/library/bb397696.aspx). But C# is a strongly typed language… not an untyped language like javascript. So creating a new class is the way to do this in C#. Commented May 18, 2015 at 22:42
  • Can you post this an answer so I can mark it as accepted when I get home and test this works on my code (which I think it will, or I think...) Commented May 18, 2015 at 22:49

2 Answers 2

39

You could create a dynamic object (https://msdn.microsoft.com/en-us/library/system.dynamic.expandoobject%28v=vs.110%29.aspx, https://msdn.microsoft.com/en-us/library/bb397696.aspx). But C# is a strongly typed language… not an untyped language like javascript. So creating a new class is the way to do this in C#.

Example using a dynamic Object:

public class Program
{
    static void Main(string[] args)
    {
        var colors = new { Yellow = ConsoleColor.Yellow, Red = ConsoleColor.Red };
        Console.WriteLine(colors.Red);
    }
}

Or using a ExpandoObject:

public class Program
{
    static void Main(string[] args)
    { 
        dynamic colors = new ExpandoObject();
        colors.Red = ConsoleColor.Red;
        Console.WriteLine(colors.Red);
    }
}

Or the more C# OO way of doing this…. create a class:

public class Program
{
    static void Main(string[] args)
    { 
        var colors = new List<Color>
        {
            new Color{ Color = ConsoleColor.Black, Name = "Black"},
            new Color{ Color = ConsoleColor.Red, Name = "Red"},
        }; 
        Console.WriteLine(colors[0].Color);
    }
}

public class Color
{
    public ConsoleColor Color { get; set; }
    public String Name { get; set; }
}

I recommend using the last version.

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

1 Comment

Worth noting: with the advent of C# 7, you can return Tuples with named items as detailed in this answer. This lets you return something like the first dynamic object in this answer from a function, if you like.
8

Sometimes Google is your best friend:

https://msdn.microsoft.com/en-us/library/bb397696.aspx

var Colors = new {
    Blue = Xamaring.Color.FromHex("FFFFFF"),
    Red = Xamarin.Color.FromHex("F0F0F0")
}

9 Comments

I don't think that works. How can I create a new "Blue" if Blue doesn't exist as a class?
It works...blue is just the name of the property the type is given from the right side. Try: var colors = new { Blue = ConsoleColor.White, Red = ConsoleColor.Red };
@NathanCooper The above code might not compile (Because I dont have a C# compiler at the moment) but Google and Microsoft defenitly are a better way of finding information than testing until it works. @musium I will edit the answer to use var instead, didn't really look through the microsoft site :| @sgarcia you are not creating new Blue();s you are not even creating new Xamarin.Color's. All you are doing is putting objects in a parent object, just like how javascript works. (there is no such thing as a new {}; in javascript, you just put in existing variables: {x: 5} does work.)
Now your code does compile, but is a local variable that cannot be returned from the scope is in declared in in a typesafe manner. It also cannot be set as a member of a class. It really does depend on what the OP means by "later", but this isn't going to be the right approach for variables needed after the method scope colors was declared in is exited. ("Compilers enforce understanding" was nonsense of course, but I'm still a big fan of them, there's even a great .NET one online: dotnetfiddle.net)
@YoYoYonnY Just for the record. There is a new key word in javascript developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… But you’re right no new object (expect the parent object) gets created in your code.
|

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.