c# 属性

c#进阶
属性

public static int Num
{
  get;//只读
  set;//只写
}

public static int Num
{

  get
  {
    return num;
	
  }//只读
  set
  {
    num = value;
  }//只写
  //关于属性,应该在适当的时候对他进行初始化
  
}

 static void Main(string[] args)
 {
   Num = 100;
	 Console.WriteLine("hello world" Num);
	 Console.ReadLine();
 }



泛型

使用:我们在写代码的时候经常会遇到逻辑一样的代码,只有数据类型不同,这时候哦们可以通过使用泛型来让代码更高效.

	static void SwapInt(ref int a,ref int b){
    int temp;
	temp = a;
	a = b;
	b = temp;
}
	static void SwapInt(ref float a,ref float b){
    float temp;
	temp = a;
	a = b;
	b = temp;
}

static void Main(string[] args)
{
  Console.WriteLine("交换前 a:{0} b:{1}",a,b);
	SwapInt(ref a, ref b);
	Console.WriteLine("交换后a:{0} b:{1}",a.b)
		Console.ReadLine();
	int 为值类型,下方修改并不能更改其真正的值,只会重新在栈上开辟一段内存,若想改变其值,必须使用引用类型,需要加ref,传递引用型参数.
}

泛型

  把一个类型当成一个参数,就是泛型
static void SwapInt<T>(ref T a,ref T b)//泛型的声明并不需要注明使用的是什么类型,用的时候指定他的数据类型
  {
    T temp;
	temp = a;
	a = b;
	b = temp;
}
Swap<int>(ref a,ref b);

类委托接口皆可以使用泛型

泛型类:
class MyTest<T>{}//泛型类声明
MyTest<T> strs = new MyTest<string>(2);//泛型类的使用方法,2表示两个数据
strs.test[0] = "hello";//0表示第0位
strs.test[1] = "world";//1表示第一位
static void Swap<T>(ref T a,ref T b)
{
  
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值