Christian Orth
Christian Orth

Reputation: 4673

Name Parameters dynamically?

Just imagine this simple situation:

int temperature = 20;

is there any possibility to give a dynamic parameter name?? Like for example (I know that doesn't work, just to get the idea) :

int Array[1].ToString() = 20;

Thank you! Cheers Chris

Upvotes: 0

Views: 814

Answers (3)

Tamer
Tamer

Reputation: 84

try this

using System;

class Program
{
    static void Foo(dynamic duck)
    {
        duck.Quack(); // Called dynamically
    }

    static void Foo(Guid ignored)
    {
    }

    static void Main()
    {
        // Calls Foo(dynamic) statically
        Foo("hello");
    }
}

and follow that link Link1

Upvotes: 0

CraigW
CraigW

Reputation: 715

Not sure if this is what you are looking for but you can play with the dynamic/Expando objects in C# 4

http://blogs.msdn.com/b/csharpfaq/archive/2009/10/01/dynamic-in-c-4-0-introducing-the-expandoobject.aspx

Upvotes: 2

Justin Niessner
Justin Niessner

Reputation: 245449

No. Not in C#. The closest you could get would be to use a Dictionary<string, object>:

var variables = new Dictionary<string, object>();

variables.Add(Array[1].ToString(), 20);

But not only does that involve casting each time you need to retrieve the value, but it will also cause boxing/unboxing.

All told, I certainly wouldn't recommend it.

Upvotes: 2

Related Questions