标签:
定义类的方法
//要定义对象,必须有其相应的类。类就是描述一些具有相同属性的事物的抽象概念。比如我们定义一个人员类。如下
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 };
动态长度数组
namespaceArrayManipulation{Class Program{staticvoidMain (String[] args){int[] arr =newint[]{1,2,3};PrintArr(arr);arr = (int[])Redim(arr,5);PrintArr (arr);arr = (int[]) Redim (arr, 2);PrintArr (arr);)publicstaticArray Redim (Array origArray,intdesiredSize){//determine the type of elementType 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 arrayArray newArray = Array.CreateInstance (t, desiredSize);//copy the original elements of the array to the new arrayArray.Copy (origArray, 0, newArray, 0, Math.Min (origArray.Length, desiredSize));//return new arrayreturnnewArray;}//print arraypublicstaticvoidPrintArr (int[] arr){foreach(intxinarr){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的
}
}
}
http://kooyee.iteye.com/blog/350017
int [ ][ ] b = new int [ 4][ ]; 最后的方括号内不写入数字
b [ 0 ] = new int [ 4];
b [1 ] = new int [5]; --这几行代码是定义每一行中的元素的个数。
b [2 ] = new int [2];
b [ 0][ 1] = 10; ---赋值。
标签:
原文地址:http://my.oschina.net/u/2360054/blog/500735