C# 对象初始化问题(可否像C语言的结构一样,简洁的初始化)

来源:互联网 发布:js 固定长度数组 编辑:程序博客网 时间:2024/06/04 18:48

C# 语言的结构、对象,可否像C语言的结构一样,简洁的初始化?一般是不可以的。经过一系列复杂的“加工”,C#对象是可以的。

C语言的结构,可以用简洁的方式初始化,

例如 struct a = { 1 , "abc"};

C#  语言的对象,是否也可以使用类似的方式呢?

例如:Class_a_b myClass_ab = new Class_a_b { 1, "2" };

收集网络上的各种信息,杂凑出了如下的代码:(当然,使用自定义的构造函数,用括号而不是花括号的方法,很容易就实现“简易”初始化)

  

sing System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Collections;namespace ConsoleApplication2{    class Class_a_b : IEnumerable //只能用 class 不能用 struct ,不知道为什么    {        public int field_int;        public string field_string; //在这个类中,就这两个域是“有用”的数据        //下面的语句都是为了“简单初始化”而服务的        // 最终就是为了实现: Class_a_b myClass_ab = new Class_a_b { 1, "2" };        int current = 0;  //如果不是class,而是 struct 结构,这句会初始化赋值语句,会出错        public IEnumerator GetEnumerator()        {            return new TokenEnumerator(this);        }        public void Add(object aa)        {            if (current++ == 0)                field_int = (int)aa;            else                field_string = (string)aa;        }        private class TokenEnumerator : IEnumerator        {            private int position = -1;            private Class_a_b t;            public TokenEnumerator(Class_a_b t)            {                this.t = t;            }            // Declare the MoveNext method required by IEnumerator:            public bool MoveNext()            {                if (position < 1)                {                    position++;                    return true;                }                else                {                    return false;                }            }            // Declare the Reset method required by IEnumerator:            public void Reset()            {                position = -1;            }            // Declare the Current property required by IEnumerator:            public object Current            {                get                {                    if (position == 0) return t.field_int;                    else return t.field_string;                }            }        }    }//测试一下    class Program    {        static void Main(string[] args)        {//终于实现了如下的语句:            Class_a_b myClass_ab = new Class_a_b { 1, "2" }; //这个就是传说中的“简单初始化”,是不是“相当的简单啊!”            int AA = myClass_ab.field_int; //在这里设置断点,检查结果            string bb = myClass_ab.field_string;        }    }}


再补充一个struct List的例子,使用花括号初始化自定义的Class类型:代码来自网络


    public struct MyUI //这是要初始化的结构    {        string A;        string B;        int C;        public MyUI(string a, string b, int c)        {            A = a;            B = b;            C = c;        }    }    public class MyList : IEnumerable<MyUI>  // note: using System.Collections;    {        private List<MyUI> lst = new List<MyUI>(); //这个是关键,创建了一个List        public void Add(string a, string b, int c)        {            lst.Add(new MyUI(a, b, c));        }        public IEnumerator<MyUI> GetEnumerator()        {            return lst.GetEnumerator();        }        IEnumerator IEnumerable.GetEnumerator()        {            return lst.GetEnumerator();        }    }    //这里,关键是要实现 IEnumerable,同时有一个 Add 方法用于匹配 { ....} 表达式。    //最终可以这样调用其实例化和Add方法。测试一下:    public class Test    {        MyList m_pList = new MyList        {            {"asd", "234", 2},            {"k2j", "298", 3}        };        //MyUI struct_test = new MyUI { "k2j", "298", 3 }; //error!!!    }


原创粉丝点击