当前位置:首页 > Windows程序 > 正文

C#的基础内容

2021-03-26 Windows程序

定义类的方法

//要定义对象,必须有其相应的类。类就是描述一些具有相同属性的事物的抽象概念。比如我们定义一个人员类。如下 class Person {   public string name;   public int age;   public string sex;   public Person()   {   } } //然后定义这个类的对象。

定义一个数组

int[] myIntArray; myIntArray = new int[5]; const int arraySize = 5; int[] myIntArray = new int[arraySize] { 5, 9, 10, 2, 99 };

动态长度数组


namespace ArrayManipulation

{

Class Program

{

static void Main (String[] args)

{

int [] arr = new int []{1,2,3};

PrintArr(arr);

arr = ( int [])Redim(arr,5);

PrintArr (arr);

arr = ( int []) Redim (arr, 2);

PrintArr (arr);

)

public static Array Redim (Array origArray, int desiredSize)

{

//determine the type of element

Type t = origArray.GetType().GetElementType();

//create a number of elements with a new array of expectations

//new array type must match the type of the original array

Array newArray = Array.CreateInstance (t, desiredSize);

//copy the original elements of the array to the new array

Array.Copy (origArray, 0, newArray, 0, Math.Min (origArray.Length, desiredSize));

//return new array

return newArray;

}

//print array

public static void PrintArr ( int [] arr)

{

foreach ( int x in arr)

{

Console.Write (x + "," );

}

Console.WriteLine ();

}

}

}





 

?

调用类的方法

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Exeplems { public class Class1 { public static void Method() { Console.WriteLine("静态的方法可以直接调用!但是很耗资源!"); } public void Method1() { Console.WriteLine("非静态的方法要创建对象来访问!"); } public void Method2() { Method1();//同一类中的方法体可以直接访问静态和非静态的 Method(); } } class Program { static void Main(string[] args) { Class1.Method();//访问静态方法 Class1 obj = new Class1();//创建对象 obj.Method1();//访问非静态方法;必须是public的 } } } 数组作为参数的语法

数组的嵌套
  int [ ][ ] b = new  int [ 4][ ];  最后的方括号内不写入数字
     b [ 0 ] = new int [ 4];
     b [1 ] = new int [5];              --这几行代码是定义每一行中的元素的个数。
     b [2 ] = new int [2];
     b [ 0][ 1] = 10;  ---赋值。

 

标签:

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/67780.html