static void Main(string[] args)
{
point p1=new point (2,3);
point.twice(out p, p1);
Console.WriteLine(p.x);
Console.WriteLine(p.y);
Console.ReadLine();
}
class point
{
public double x;
public double y;
public point(double a, double b)
{
x = a;
y = b;
} public static void twice(out point p,point pp)
{
p.x = pp.x*2;
p.y = pp.y*2; }
}总是报错“使用了未赋值的变量‘p’”
请问如何修改,将输入点的X,y扩大两倍,通过OUT方式传出
{
point p1=new point (2,3);
point.twice(out p, p1);
Console.WriteLine(p.x);
Console.WriteLine(p.y);
Console.ReadLine();
}
class point
{
public double x;
public double y;
public point(double a, double b)
{
x = a;
y = b;
} public static void twice(out point p,point pp)
{
p.x = pp.x*2;
p.y = pp.y*2; }
}总是报错“使用了未赋值的变量‘p’”
请问如何修改,将输入点的X,y扩大两倍,通过OUT方式传出
加上Point p=new Point(0,0);试试
public static void twice(ref point p)
{
if (p == null)
return;
p.x *= 2.0;
p.y *= 2.0;
}
class Demo
{
static void Main(string[] args)
{
Point p1 = new Point(2, 3);
Point p;
Point.Twice(out p, p1);
Console.WriteLine(p.x);
Console.WriteLine(p.y);
Console.ReadLine();
}
}class Point
{
public double x;
public double y;
public Point(double a, double b)
{
x = a;
y = b;
} public static void Twice(out Point p, Point pp)
{
p = new Point(pp.x * 2, pp.y * 2);
}
}
这是ref和out的区别
{
return new Point(p.x * 2, p.y * 2);
}
class testapp
{
static void outtest(out int x, out int y)
{//离开这个函数前,必须对x和y赋值,否则会报错。
//y = x;
//上面这行会报错,因为使用了out后,x和y都清空了,需要重新赋值,即使调用函数前赋过值也不行
x = 1;
y = 2;
}
static void reftest(ref int x, ref int y)
{
x = 1;
y = x;
}
public static void Main()
{
//out test
int a,b;
//out使用前,变量可以不赋值
outtest(out a, out b);
console.writeline("a={0};b={1}",a,b);
int c=11,d=22;
outtest(out c, out d);
Console.WriteLine("c={0};d={1}",c,d); //ref test
int m,n;
//reftest(ref m, ref n);
//上面这行会出错,ref使用前,变量必须赋值 int o=11,p=22;
reftest(ref o, ref p);
Console.WriteLine("o={0};p={1}",o,p);
}
}