C#學(xué)習(xí)筆記- 淺談數(shù)組復(fù)制,排序,取段,元組
更新時(shí)間:2016年08月21日 15:00:48 投稿:jingxian
下面小編就為大家?guī)硪黄狢#學(xué)習(xí)筆記- 淺談數(shù)組復(fù)制,排序,取段,元組。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
C#學(xué)習(xí)筆記- 淺談數(shù)組復(fù)制,排序,取段,元組
using System; using System.Collections.Generic; namespace Application { class Test { static void Main () { //元組類型Tuple是靜態(tài)類型,用靜態(tài)方法創(chuàng)建實(shí)例,超過8個(gè)元素則第8個(gè)元素是元組類型 var tupe = Tuple.Create<int,int,string,string> (1, 2, "a", "b"); Console.WriteLine ("{ 0},{ 1}",tupe.Item1, tupe.Item3); //=====Array類是抽象類,只能通過它的靜態(tài)方法CreateInstance()創(chuàng)建實(shí)例 //=====如果事先不知道類型,可以用此方法 Array arrays = Array.CreateInstance (typeof(int), 5); for (int i = 0; i < arrays.Length; i++) { arrays.SetValue (35, i); Console.WriteLine (arrays.GetValue (i)); } Person[] persons = { new Person { firstName = "su", lastName = "uifu" }, new Person { firstName = "chen", lastName = "xaohua" }, new Person { firstName = "cbb", lastName = "ruifu" }, new Person { firstName = "utt", lastName = "xiaohua" } } ; //=====Clone()復(fù)制數(shù)組,引用類型復(fù)制索引值,值類型復(fù)制值 Person[] persons1 = persons.Clone (); Array.Sort (persons, new PersonComparer (PersonCompareType.lastName)); foreach (var p in persons) { Console.WriteLine (p); } //======ArraySegment<T>對(duì)數(shù)組取段==== var segments = new ArraySegment<Person> (persons, 1, 2); for (int i = segments.Offset; i < segments.Offset + segments.Count; i++) { Console.WriteLine (segments.Array [i]); } } public class Person { public string firstName{ get; set; } public string lastName{ get; set; } public override string ToString () { return String.Format ("{ 0},{ 1}", firstName, lastName); } } //======要對(duì)引用類型的數(shù)組使用Array.sort()方法,必須對(duì)類實(shí)現(xiàn)IComparable<T>接口 //======或?qū)懸粋€(gè)附加類并實(shí)現(xiàn)Comparer<T>接口 public enum PersonCompareType { firstName, lastName } public class PersonComparer:IComparer<Person> { private PersonCompareType pct; public PersonComparer (PersonCompareType pct) { this.pct = pct; } public int Compare(Person x,Person y) { if (x == null) throw new ArgumentNullException (); if (y == null) throw new ArgumentNullException (); switch (pct) { case PersonCompareType.firstName: return x.firstName.CompareTo (y.lastName); case PersonCompareType.lastName: return x.lastName.CompareTo (y.lastName); default: throw new ArgumentException ("no.."); } } } }
以上這篇C#學(xué)習(xí)筆記- 淺談數(shù)組復(fù)制,排序,取段,元組就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Unity UGUI實(shí)現(xiàn)簡(jiǎn)單拖拽圖片功能
這篇文章主要為大家詳細(xì)介紹了Unity UGUI實(shí)現(xiàn)簡(jiǎn)單拖拽圖片功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06c# Newtonsoft 六個(gè)值得使用的特性(下)
這篇文章主要介紹了c# Newtonsoft 六個(gè)值得使用的特性,文中示例代碼非常詳細(xì),幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-06-06c#高效率導(dǎo)出多維表頭excel的實(shí)例代碼
這篇文章介紹了c#高效率導(dǎo)出多維表頭excel的實(shí)例代碼,有需要的朋友可以參考一下2013-11-11如何使用Dapper處理多個(gè)結(jié)果集與多重映射實(shí)例教程
Dapper類是一個(gè)開源的數(shù)據(jù)庫操作類,下面這篇文章主要給大家介紹了關(guān)于如何使用Dapper處理多個(gè)結(jié)果集與多重映射的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-09-09