【C#】DAY4
程序员文章站
2022-07-14 18:04:47
...
1.属性访问器
namespace test
{
public class visit
{
private int intval = 1;
public int val//访问器
{
get { return intval; }
set { intval = value; }//value是关键字,不可更改
}
}
class Program
{
static void Main(string[] args)
{
visit test = new visit();
WriteLine(test.val);//调用访问器
test.val = 2;
WriteLine(test.val);
}
}
}
namespace test
{
public class Name
{
public string name;
public Name() { name = "CN"; }
public Name(string n) { name = n; }
}
class Program
{
static void Main(string[] args)//链表:任意类型关联,注意使用规定方法
{
int n1 = 1;
string[] nmore = { "NO","YES" };
Name m1 = new Name();
Name m2 = new Name("CYN");
ArrayList L = new ArrayList();//创建链表
L.Add(n1);L.Add(2);L.Add(m1);L.Add(m2);//添加节点
WriteLine("List Length is: " + L.Count);//链表节点数
WriteLine((int)L[0]); WriteLine((int)L[1]);
WriteLine(((Name)L[2]).name); WriteLine(((Name)L[3]).name);//输出链表内容要转换类型
L.RemoveAt(2);L.Remove(n1);//删除节点
L.AddRange(nmore);//批量添加节点
WriteLine("YES is at 3 ?" + (string)L[3]);
WriteLine("CYN is at: " + L.IndexOf(m2));//查询节点位置
}
}
}
3.“is”判定字
if(3 is int) { WriteLine("3 is integer"); }//“is”判断是否是指定类型
4.重载运算符
//重载运算符
public static AddClass operator+(AddClass a1,AddClass a2)
{
AddClass returnVal=new AddClass();
returnVal.val=a1.val+a2.val;
return returnVal;
}