How do I name variables dynamically in C#?

江枫思渺然 提交于 2019-11-26 20:31:52

Use a Dictionary<string, Variable>.

e.g.

var vartable = new Dictionary<string, Variable>();
vartable[strLine] = new Variable(input);

C# 4.0, using the dynamic objects:

dynamic d = new ExpandoObject();
((IDictionary<string, object>)d)["MyProperty"] =  5;
int val = d.MyProperty; // 5

No, but you could use a Dictionary<string, Variable>, and then you can refer to each variable by its quoted name.

You can't do that, but what you're looking to do is begging for a Dictionary use:

Dictionary<object, object> d = new Dictionary<string, object>();
d.Add("Variable1", value1);
d.Add("Variable2", value2);
d.Add("Variable3", value3);

try this one,user json to serialize and deserialize:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Web.Script.Serialization;

    namespace ConsoleApplication1
    {
     public class Program
     {
        static void Main(string[] args)
        {
            object newobj = new object();

            for (int i = 0; i < 10; i++)
            {
              List<int> temp = new List<int>();

              temp.Add(i);
              temp.Add(i + 1);

               newobj = newobj.AddNewField("item_" + i.ToString(), temp.ToArray());
            }

       }

    }

    public static class DynamicExtention
    {
        public static object AddNewField(this object obj, string key, object value)
        {
            JavaScriptSerializer js = new JavaScriptSerializer();

            string data = js.Serialize(obj);

            string newPrametr = "\"" + key + "\":" + js.Serialize(value);

            if (data.Length == 2)
           {
               data = data.Insert(1, newPrametr);
            }
          else
            {
                data = data.Insert(data.Length-1, ","+newPrametr);
            }

            return js.DeserializeObject(data);
        }
     }
    }

Variable names should be known at compile time. If you intend to populate those names dynamically at runtime you could use a List<T>.

 var variables = List<Variable>();
 variables.Add(new Variable { Name = input1 });
 variables.Add(new Variable { Name = input2 });
 ...

No. You can load them into a Dictionary object, however. This allows you to still reference them (somewhat) using a name, which is a bit easier than using an Index, as you would with an Array or ArrayList.

I would use some sort of keyed collection, like a hashtable, dictionary, or list of structures with the name. You can then refer to the variable by name:

var value = variableDictionary["MyVariableName"];
var value = variableHashtable["MyVariableName"];
var value = variableList.First(x=>x.Name == "MyVariableName");

There is no other way to dynamically "name" a variable.

The GiG

No. Use an array, otherwise you can't do this.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!