当前位置: 移动技术网 > IT编程>开发语言>c# > C#中数组初始化、反转和排序用法实例

C#中数组初始化、反转和排序用法实例

2019年07月18日  | 移动技术网IT编程  | 我要评论

本文实例讲述了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#程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网