<i id='SUosz'><tr id='SUosz'><dt id='SUosz'><q id='SUosz'><span id='SUosz'><b id='SUosz'><form id='SUosz'><ins id='SUosz'></ins><ul id='SUosz'></ul><sub id='SUosz'></sub></form><legend id='SUosz'></legend><bdo id='SUosz'><pre id='SUosz'><center id='SUosz'></center></pre></bdo></b><th id='SUosz'></th></span></q></dt></tr></i><div id='SUosz'><tfoot id='SUosz'></tfoot><dl id='SUosz'><fieldset id='SUosz'></fieldset></dl></div>

        <small id='SUosz'></small><noframes id='SUosz'>

      1. <tfoot id='SUosz'></tfoot>

        • <bdo id='SUosz'></bdo><ul id='SUosz'></ul>
      2. <legend id='SUosz'><style id='SUosz'><dir id='SUosz'><q id='SUosz'></q></dir></style></legend>

        C# 语法通过数组中的构造函数参数初始化自定义类/对象?

        时间:2023-11-10
        <tfoot id='7N3MC'></tfoot>
      3. <legend id='7N3MC'><style id='7N3MC'><dir id='7N3MC'><q id='7N3MC'></q></dir></style></legend>
          <bdo id='7N3MC'></bdo><ul id='7N3MC'></ul>
        • <i id='7N3MC'><tr id='7N3MC'><dt id='7N3MC'><q id='7N3MC'><span id='7N3MC'><b id='7N3MC'><form id='7N3MC'><ins id='7N3MC'></ins><ul id='7N3MC'></ul><sub id='7N3MC'></sub></form><legend id='7N3MC'></legend><bdo id='7N3MC'><pre id='7N3MC'><center id='7N3MC'></center></pre></bdo></b><th id='7N3MC'></th></span></q></dt></tr></i><div id='7N3MC'><tfoot id='7N3MC'></tfoot><dl id='7N3MC'><fieldset id='7N3MC'></fieldset></dl></div>

                  <small id='7N3MC'></small><noframes id='7N3MC'>

                    <tbody id='7N3MC'></tbody>

                  本文介绍了C# 语法通过数组中的构造函数参数初始化自定义类/对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个至少有 4 个变量的类,并且我为该类创建了一个构造函数,以便我可以使用它来初始化它

                  I have a class with minimum 4 variables and I have made a constructor for the class so that I can initialize it with

                  MyClass testobj = new MyClass(1234,56789,"test text", "something else", "foo");
                  

                  工作正常.

                  然后我有一个数组,我需要在一个循环中解析,所以我想将一些静态数据放入这个数组中.

                  Then I have an array of these, that I need to parse in a loop, so I would like to get some static data into this array.

                  我的做法是:

                  MyClass[] testobjlist = new MyClass 
                  {
                       new MyClass(1001,1234,"Text 1", "abcdefghijklm", "ding"),
                       new MyClass(1002,2345,"Text xx", "bla bla", "dong"),
                       new MyClass(1003,8653,"Text yy", "blah blah even more", "bamm!")
                  }
                  

                  但不知何故,这给了我一个奇怪的错误,关于我需要一个额外的 } ???

                  but somehow this gives me a weird error about me needing an extra } ???

                  我不知道该不该提这个,但是我用 Razor-engine 2 做网页.不过我觉得这是一个普通的 C# 问题?

                  I don't know if I should mention this, but I use it for webpages using Razor-engine 2. But I think this is an ordinary C# question?

                  我的解决方法目前是用一个大小初始化数组,然后通过索引一个一个地添加元素,但我更喜欢上面的解决方案,因为我可能必须在测试时按顺序上下移动项目,我实际数据中的数量远远超过 3.

                  My workaround is currently to initialize the array with a size, then adding the elements one by one through index, but I would rather prefere the above solution as I might have to move the items up and down in order when testing and I have a lot more than 3 in the real data.

                  我在上面的代码中缺少什么?

                  What I am missing in the above code?

                  推荐答案

                  尝试在new MyClass后面加方括号,最后加分号

                  Try adding square brackets after new MyClass and a semi-colon at the end

                      MyClass[] testobjlist = new MyClass[] 
                          {
                           new MyClass(1001,1234,"Text 1", "abcdefghijklm", "ding"),
                           new MyClass(1002,2345,"Text xx", "bla bla", "dong"),
                           new MyClass(1003,8653,"Text yy", "blah blah even more", "bamm!")
                          };
                  

                  这篇关于C# 语法通过数组中的构造函数参数初始化自定义类/对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在 C# 中内联或在静态构造函数中初始化静态变量的区别 下一篇:“信号量超时期限已过";USB连接错误

                  相关文章

                  • <bdo id='YL9Ok'></bdo><ul id='YL9Ok'></ul>
                  <i id='YL9Ok'><tr id='YL9Ok'><dt id='YL9Ok'><q id='YL9Ok'><span id='YL9Ok'><b id='YL9Ok'><form id='YL9Ok'><ins id='YL9Ok'></ins><ul id='YL9Ok'></ul><sub id='YL9Ok'></sub></form><legend id='YL9Ok'></legend><bdo id='YL9Ok'><pre id='YL9Ok'><center id='YL9Ok'></center></pre></bdo></b><th id='YL9Ok'></th></span></q></dt></tr></i><div id='YL9Ok'><tfoot id='YL9Ok'></tfoot><dl id='YL9Ok'><fieldset id='YL9Ok'></fieldset></dl></div>

                    1. <legend id='YL9Ok'><style id='YL9Ok'><dir id='YL9Ok'><q id='YL9Ok'></q></dir></style></legend>

                      <small id='YL9Ok'></small><noframes id='YL9Ok'>

                    2. <tfoot id='YL9Ok'></tfoot>