C#中數組初始化、反轉和排序用法實例。本站提示廣大學習愛好者:(C#中數組初始化、反轉和排序用法實例)文章只能為提供參考,不一定能成為您想要的結果。以下是C#中數組初始化、反轉和排序用法實例正文
本文實例講述了C#中數組初始化、反轉和排序用法。分享給年夜家供年夜家參考。詳細以下:
上面的代碼演示了在C#中界說和初始化數組,然後對其停止賦值,排序和反轉的操作辦法:
using System;
public class ArraySample
{
public static void Main()
{
// Create and initialize a new array instance.
Array strArr = Array.CreateInstance(typeof(string), 3);
strArr.SetValue("Mahesh", 0);
strArr.SetValue("chand", 1);
strArr.SetValue("Test Array", 2);
// Display the values of the array.
Console.WriteLine("Initial Array values:");
for(int i = strArr.GetLowerBound(0);i<=strArr.GetUpperBound(0);i++)
Console.WriteLine(strArr.GetValue(i));
//sort the value of the array.
Array.Sort(strArr);
Console.WriteLine("After sorting:");
for(int i = strArr.GetLowerBound(0);i<=strArr.GetUpperBound(0);i++)
Console.WriteLine(strArr.GetValue(i));
// Reverse values of the array.
Array.Reverse(strArr);
for(int i = strArr.GetLowerBound(0);i<=strArr.GetUpperBound(0);i++)
Console.WriteLine(strArr.GetValue(i));
}
}
願望本文所述對年夜家的C#法式設計有所贊助。