default关键词
为默认的意思
值类型为0,引用类型为null
比如:string mystring = new string();
用default表示:string mystring = default(string)
string mystring = null
用值类型也一样: float myfloat = new float();
用default表示:float myfloat = default(float)
float myfloat = 0.0f
如果用在泛型方法中则要在给泛型添加约束
因为代码会出现错误
比如:
static class Team<T>
{
public static T GerPerson<T>()
{
T t = null;
return t;
}
{
如果为值类型的话带入就是
int t = null
但这在C#中会运行错误
所以需要添加一些约束来确定他的值是值类型还是引用类型
还有就是
class Person
{
public string name;
}
class Doctor:Person
{
}
static class Team
{
private static Doctor doctorWang;
这个也叫向上转型
⬇
Person temp = doctorWang as Person
向下转型
⬇
(Doctor)temp
}
带括号的都算强制转换
没有括号的才叫隐式转换