引用参数
修改一个参数名的时候 如何快速修改其他参数名:
“CTRL + 。”
如果给方法参数传引用之后,方法体内部有new关键字,表示重新创造了一个对象
Object.GetHashCode()方法可以获得每个对象的hashcoude,都不一样
ref 引用参数修饰符 -这里还是有一些内容的,不过根据需求来吧
out 输出参数修饰符
引用类型的输出参数例子
class Program
{
static void Main(string[] args)
{
//声明一个学生实例
Student stu = null;
//工厂类的静态方法,返回bool值,输出参数为学生实例
bool b = StudentFactory.CreateStu("tom", 34, out stu);
if (b==true)
{
Console.WriteLine("student {0},age is {1}",stu.MyNmae,stu.MyAge);
}
}
}
class Student
{
public string MyNmae { get; set; }
public int MyAge { get; set; }
}
class StudentFactory
{
public static bool CreateStu(string name, int age, out Student stu)
{
stu = null;
if (name == null)
{
return false;
}
if (age<20 || age>80)
{
return false;
}
stu = new Student() { MyAge = age, MyNmae = name };
return true;
}
}
数组参数 params 修饰符
具名参数
class Program
{
static void Main(string[] args)
{
Some(Name:"tom",ID:12);
}
public static void Some(string Name,int ID)
{
Console.WriteLine("{0},{1}",Name,ID);
}
}
扩展方法(this参数)
方法必须是共有的、静态的,即被public static 所修饰
必须是形参列表中的第一个,由this修饰
必须由一个静态类(一般类名为SomeTypeExtension)来统一收纳对SomeType类型的扩展方法
举例 LINQ
class Program
{
static void Main(string[] args)
{
double x = 3.141592;
double y = x.Round(4);
Console.WriteLine(y);
}
}
static class DoubleExtension
{
public static double Round(this double input,int digits)
{
double result = Math.Round(input, digits);
return result;
}
}