C#学习笔记20160512

本文介绍了C#中的基本语法,包括类型判断、对象创建、字符串操作等,并详细讲解了数组、ArrayList等数据结构的使用方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

bool result = i is int; //判断该变量是否为 这个类型,返回true / false

new运算符用于创建一个新的类型实例, 有三种形式:

1. 对象创建 ,用于创建 一个类类型或值 类型的实 例 。

2. 数组创建 。

3. 代表创建表达式。


typeof 运处符用于获得系统 原型对象的类型。

Type sysType = typeof(int);   // System.Int32


Char 在c#中表示 一个Unicode字符。

Is开头的方法大多是判断Unicode字符是否为某个类别。

To开头的方法主要是转换为其他Unicode字符。


Compare比较两个字符串是否相等。

CompareTo方法与Compare相似,但该方法是字符串本身调用

Equals判断两个字符中是否相同。

public static string Format(string format, object obj); 格式化字符串。


public string Substring(int startIndex, int length) 截取字答串,从startIndex开始截取length个字符。

public string[] split(params char[] separator); 分割字符串。

public string insert(int startIndex, string value); 插入字符串。

public string PadLeft(int totalWidth, char paddingChar);

public string PadRight(int totalWidth, char paddingChar);

以上两个,填 充左侧或者 右侧 多少 个字符。totalWidth,指定填 充后的长度。

public string Remove(int startIndex);

public string Remove(int startIndex, int count);

删除字符串。


public static string Copy(string str) //复制字符串

public void copyTo(int sourceIndex, char[] destination, int destinationIndex, int count); //将字符串中某一部分复制到一个数组中。


public string Replace(char OChar, char NChar)

public string Replace(string OValue, string NValue);

替换字符串。


可变字符串类StringBuilder, 在System.Text命名空间中。

public StringBuilder(string value, int cap); //常用的构建方法。

String对象是不可改变的,每次使用String 类中的方法时,都要在内存中创建一个新的字符串对象,这就需要为该新对象分配新的空间。在需要 对字符串执行重复修改的情况下,与创建新的String对象相关的系统开销可能会非常昂贵。如果要修改字符串而不创建新的对象。则可以使用StringBuilder类。例如 ,当在一个循环中将许多字符串连接在一起时,使用StringBuilder类可以提升性能。


foreach 语句

foreach (【类型]】 【迭代变量名】  in 【集合类型表达式】)

{

【语句块】;

}


一维数组的声明

type[]  arrayName;

int arr = new int[5];

二维数组的声明

type[,]  arrayName;

int [,]  arr = new int[2,2];


动态二维数组的声明及使用

type[] arrayName;

arrayName = new type[n1, n2, ...];


int  m = 2;

int  n = 2;

int[,]  array2 = new int[m, n];



数组排序方法:

Array.Sort(arr); //

Array.Reverse() //反向排序



ArrayList位于System.Collections命名空间下。

构造方法:

public ArrayList();

ArrayList List = new  ArrayList();

public ArrayList(ICollection);


ArrayList的常用属性及说明

Capacity  获取或设置可 包含的元素个数。

Count   获取实际包含的元素数。

IsFixedSize 是否具有固定大小

IsReadOnly 是否可读 

IsSynchronized  是否同步对ArrayList的访问

Item 获取或设置指定索引处的元素

SyncRoot 可用于同步ArrayList访问的对象。












评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值