Create dynamic variable name

前端 未结 5 1497
太阳男子
太阳男子 2020-11-22 07:56

Can we create dynamic variable in C#?

I know my below code is threw error and very poor coding. But this code have small logic like create dynamic variable

相关标签:
5条回答
  • 2020-11-22 08:06

    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 = inputStr1 });
     variables.Add(new Variable { Name = inputStr2 });
    

    here input string maybe any text or any list

    0 讨论(0)
  • 2020-11-22 08:14

    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);
              }
          }
       }
    
    0 讨论(0)
  • 2020-11-22 08:16

    No. That is not possible. You should use an array instead:

    name[i] = i;
    

    In this case, your name+i is name[i].

    0 讨论(0)
  • 2020-11-22 08:25

    This is not possible, it will give you a compile time error,

    You can use array for this type of requirement .

    For your Reference :

    http://msdn.microsoft.com/en-us/library/aa288453%28v=vs.71%29.aspx

    0 讨论(0)
  • 2020-11-22 08:29

    C# is strongly typed so you can't create variables dynamically. You could use an array but a better C# way would be to use a Dictionary as follows. More on C# dictionaries here.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace QuickTest
    {
        class Program
        {
            static void Main(string[] args)
            {
                Dictionary<string, int> names = new Dictionary<string,int>();
    
    
                for (int i = 0; i < 10; i++)
                {
                    names.Add(String.Format("name{0}", i.ToString()), i);
                }
    
                var xx1 = names["name1"];
                var xx2 = names["name2"];
                var xx3 = names["name3"];
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题