C#随笔
预处理器指令
define与undefine
#define与#undef声明必须放在C#源文件的开头位置,即程序集的引用的上方,不然将报错,错误信息请看上方注意部分。
#define C
#define B
using System;
namespace HelloWorldApplication
{
class HelloWorld
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
//Console.ReadKey();
#if A
Console.WriteLine('A');
#if B
Console.WriteLine('B');
#endif
#elif C
Console.WriteLine('C');
#else
Console.WriteLine('D');
#endif
}
}
}
string
方法
IndexOf与IndexOfAny
char[] ch = { 's', 'c', 'b' };
string str = "acsdfgdfgchacscdsad";
int lbl = 0;
lbl = str.IndexOfAny(ch, 5, 5);
IndexOfAny与IndexOf类似,区别在于可以搜索在一个字符串中,出现一个字符数组中任意字符的第一次出现的位置。
函数
函数参数
private void button1_Click(object sender, EventArgs e)
{
string[] strPara = new string[]{ "hello", "Anny", "!"};
label1.Text = change_str(strPara)[1]; // 输出: ss
}
private string[] change_str(string[] str)
{
str[1] = "ss";
return str;
}
参数为引用类型,函数内的改变会影响到函数外的变量。
类
类和结构
类和结构都是创建对象的模版。类定义了类的每个对象可以包含什么数据和功能。
类
引用类型,存储在堆上。
支持继承。
类类型的对象通过引用传递。
结构
值类型,存储在栈上。
不支持继承。
结构类型的对象按值传递。
可提高性能,避免垃圾收集。
都使用关键字new声明实例,类和结构的字段值都默认为0。

浙公网安备 33010602011771号