首页 > Windows开发 > 详细

C#的基础内容

时间:2015-09-02 17:28:49      阅读:348      评论:0      收藏:0      [点我收藏+]

 

定义类的方法

//要定义对象,必须有其相应的类。类就是描述一些具有相同属性的事物的抽象概念。比如我们定义一个人员类。如下
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的
}
}
}

 

 

 

数组作为参数的语法

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;  ---赋值。

 

 

 

C#的基础内容

原文:http://my.oschina.net/u/2360054/blog/500735

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!