程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#進修筆記- 淺談數組復制,排序,取段,元組

C#進修筆記- 淺談數組復制,排序,取段,元組

編輯:C#入門知識

C#進修筆記- 淺談數組復制,排序,取段,元組。本站提示廣大學習愛好者:(C#進修筆記- 淺談數組復制,排序,取段,元組)文章只能為提供參考,不一定能成為您想要的結果。以下是C#進修筆記- 淺談數組復制,排序,取段,元組正文


C#進修筆記- 淺談數組復制,排序,取段,元組

using System;
using System.Collections.Generic;

namespace Application
{
class Test
{
static void Main ()
{

//元組類型Tuple是靜態類型,用靜態辦法創立實例,跨越8個元素則第8個元素是元組類型

var tupe = Tuple.Create<int,int,string,string> (1, 2, "a", "b");
Console.WriteLine ("{ 0},{ 1}",tupe.Item1, tupe.Item3);


//=====Array類是籠統類,只能經由過程它的靜態辦法CreateInstance()創立實例

//=====假如事前不曉得類型,可以用此辦法

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()復制數組,援用類型復制索引值,值類型復制值

Person[] persons1 = persons.Clone ();
Array.Sort (persons, new PersonComparer (PersonCompareType.lastName));
foreach (var p in persons) {
Console.WriteLine (p);
}

//======ArraySegment<T>對數組取段====
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);
}
}
//======要對援用類型的數組應用Array.sort()辦法,必需對類完成IComparable<T>接口

//======或寫一個附加類並完成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#進修筆記- 淺談數組復制,排序,取段,元組就是小編分享給年夜家的全體內容了,願望能給年夜家一個參考,也願望年夜家多多支撐。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved