Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 创建动态变量名_C# - Fatal编程技术网

C# 创建动态变量名

C# 创建动态变量名,c#,C#,我们可以在C#中创建动态变量吗 我知道我下面的代码是抛出错误和非常糟糕的编码。但是这段代码有一些小逻辑,比如创建动态变量 var name=0; for(i=0;i<10;i++)// 10 means grid length { name+i=i; } var xx1=name1; var xx2=name2; var xx3=name3; 。 . 我有一个包含多行的gridview。我需要为每一行分配服务器端变量。所以我需要服务器端的一组变量。唯一可以为每个模板字段设置T

我们可以在C#中创建动态变量吗

我知道我下面的代码是抛出错误和非常糟糕的编码。但是这段代码有一些小逻辑,比如创建动态变量

var name=0;
for(i=0;i<10;i++)// 10 means grid length
{
    name+i=i;
}

var xx1=name1;
var xx2=name2;
var xx3=name3;
。 .

我有一个包含多行的
gridview
。我需要为每一行分配服务器端变量。所以我需要服务器端的一组变量。唯一可以为每个模板字段设置
Text=

rowCount
表示每个网格行索引

如果网格有2行,则
rowCount
值为0,1,2


现在我需要为单独的行动态地将
variablename
更改为
variablename0、variablename1、variablename2

不,那是不可能的。您应该改用数组:

name[i] = i;

在这种情况下,您的
name+i
name[i]

这是不可能的,它会给您一个编译时错误

对于这种类型的需求,您可以使用数组

供参考:

C#是强类型的,因此无法动态创建变量。您可以使用数组,但更好的C#方法是使用字典,如下所示。更多关于

使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
命名空间快速测试
{
班级计划
{
静态void Main(字符串[]参数)
{
字典名称=新字典();
对于(int i=0;i<10;i++)
{
Add(String.Format(“name{0}”,i.ToString()),i);
}
变量xx1=名称[“名称1”];
变量xx2=名称[“名称2”];
变量xx3=名称[“名称3”];
}
}
}

试试这个,使用json进行序列化和反序列化:

 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);
          }
      }
   }
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Web.Script.Serialization;
命名空间控制台应用程序1
{
公共课程
{
静态void Main(字符串[]参数)
{
object newobj=新对象();
对于(int i=0;i<10;i++)
{
列表温度=新列表();
临时添加(i);
临时添加(i+1);
newobj=newobj.AddNewField(“item_”+i.ToString(),temp.ToArray());
}
}
}
公共静态类动态扩展
{
公共静态对象AddNewField(此对象对象对象、字符串键、对象值)
{
JavaScriptSerializer js=新的JavaScriptSerializer();
string data=js.Serialize(obj);
字符串newPrametr=“\”+key+“\”:“+js.Serialize(值);
如果(data.Length==2)
{
data=data.Insert(1,newpramert);
}
其他的
{
data=data.Insert(data.Length-1,“,”+newprameter);
}
返回js.DeserializeObject(数据);
}
}
}

应在编译时知道变量名。如果您打算在运行时动态填充这些名称,可以使用
列表

var variables=List();
Add(新变量{Name=inputStr1});
Add(新变量{Name=inputStr2});

这里输入的字符串可能是任何文本或任何列表

如果你问的问题有特定的
c
版本,你应该只添加它。如果你问一个关于常规
c#
的问题,只要
c#
标记就足够了。@MichaC:)我不需要数组。经过很长时间,现在我可以从我这里得到angularjs.+1。如果不使用
字典
,有可能吗?@RameshRajendran,我认为你需要更抽象地思考这个问题。根据定义,变量是一个有值的键——因此,对于不能在编译时声明的变量,使用
字典是合适的数据结构。@Michael,您对变量的定义帮助我了解到,在C#中缺少动态变量并不是一个缺点,因为我们可以改用字典。。)我用这个方法。单变量(如int、long、object等)也可以。但是,将列表集成为字典元素会导致非常慢的速度。这是非常慢的,甚至不用说多线程也会在反序列化时引起一些冲突。
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"];
        }
    }
}
 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);
          }
      }
   }
 var variables = List<Variable>();
 variables.Add(new Variable { Name = inputStr1 });
 variables.Add(new Variable { Name = inputStr2 });