[AaronYang]C#人愛學不學[5]

來源:互聯網
上載者:User

標籤:

這世上有三樣東西是別人搶不走的:一是吃進胃裡的食物,二是藏在心中的夢想,三是讀進大腦的書 --Aaronyang的部落格(www.ayjs.net)
1. 數組-的疑惑?

1.1  多維陣列       例如int[,]  a=new int[2,4]; 共兩行,每組4個。     int[,] a={{1,2,3,4},{6,7,8,9}}

1.2  鋸齒數組       例如int[][] a=new int[2][];可以確定一共2行,但每行幾個不知道,所以可以任意個。導致每行的長度可能不一致,形成一個鋸齒

1.3 不常用Array寫法   例如Array a = Array.CreateInstance(typeof(int), 5); 然後SetValue(值,位置)/GetValue(位置)     , Array.Copy()與Array.Clone()(淺複製)  Array.Sort(array數組,[比較子])

1.4 數組中如果是具有結構型的對象,比較可能不像int或者string那樣簡單,可以使用 IComparer<T>寫個比較子,網上教程很多

1.5 yield方便建立列舉程式。yield return返回集合的一個元素,並移動到下一個元素上。yield break 停止迭代

1.6 (net4.0+)  元組-   Tuple<T1,T2,T3....TResult>的使用,說白了,可以返回任意類型組裝成一個"數組"返回回去,用這個感覺很方便,不是嗎

由此也看到了,最多8個

1.7 (net4.0+) 數組和元祖都實現介面IStructuralEquatableIstructuralComparable,可以比較引用,還可以比較內容。這些介面是顯示介面。

IStructuralEquatable主要用於比較兩個元組或者數組是否有相同的內容。IstructuralComparable 用於給元組或數組排序

例子:

①定義個內容比較或者排序對比

    /// <summary>    /// aaronyang 2014年12月29日23:00:25    /// 比較 兩個房子戶型是否基本一致    /// </summary>    public class House : IEquatable<House>    {        /// <summary>        /// 面積        /// </summary>        public string MianJi { get; set; }        /// <summary>        /// 廳數        /// </summary>        public int Ting { get; set; }        /// <summary>        /// 室數        /// </summary>        public int Shi { get; set; }        public override string ToString()        {            return String.Format(MianJi+"面積,{0}室{1}廳",Shi,Ting);        }        public override bool Equals(object obj)        {            Console.WriteLine(obj.ToString());            if (obj == null)            {                return base.Equals(obj);            }            return Equals(obj as House);        }        public override int GetHashCode()        {            return base.GetHashCode();        }        public bool Equals(House other)        {            if (other == null)            {                return base.Equals(other);            }            return this.MianJi == other.MianJi && this.Ting == other.Ting && this.Shi == other.Shi;        }    }

使用對比

   static void Main(string[] args)        {            //  Array a = Array.CreateInstance(typeof(int), 5);            //Tuple<int, string, double,> a = Tuple.Create<int, string, double>(1, "aaronyang", 0.13);            //Console.WriteLine(a.Item1+"    "+a.Item2+"   "+a.i);            var house1 = new House { MianJi = "100平方", Shi = 3, Ting = 2 };            House[] houses1 ={                            new House{                             MianJi = "110平方", Shi = 3, Ting = 2                             },                             house1                            };            House[] houses2 ={                            new House{                             MianJi = "110平方", Shi = 3, Ting = 2                             },                             house1                            };            if (houses1 != houses2)            {                Console.WriteLine("兩個房子不是同一個"); //=> √            }            else {                Console.WriteLine("兩個房子不是基本一致");            }            Console.ReadKey();        }

關於比較兩個Tuple是否相等可以使用  tuple1.Equals(tuple2,new TupleComparer())                                  public class Tuple:IEqualityComparer{ ... }

 

關於數組方面的沒什麼好複習的了,感覺很多人應該沒嘗試過用Tuple,但是真的挺好用的。還有linq中經常用的對象比較子,比如linq 中distinct時候,去重複的比較子,今天就寫這麼點了,88

[AaronYang]C#人愛學不學[5]

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.