C#基础入门 四
2021-02-14 18:19
标签:区别 不同 技术 传递 float pre 避免 image 使用 求两个数的最大值 C#基础入门 四 标签:区别 不同 技术 传递 float pre 避免 image 使用 原文地址:https://www.cnblogs.com/senlinmilelu/p/8445654.htmlC#基础入门 四
方法参数
static void Car(out int x,out int y,int z){}
,与引用参数区别在于:调用方法前无需对输出参数进行初始化,输出型参数用于传递方法返回的数值。
static void rectangle(int length,int width, out int rec)
{
rec = length * width;
}
public static void Main(string[] args)
{
int a = 10;
int b = 5;
int r;
rectangle(a, b, out r);
Console.WriteLine("矩形长为"+a);
Console.WriteLine("矩形宽为"+ b);
Console.WriteLine( "面积为" + r);
}
static int rectangle(int length,int width, out int rec)
{
rec = length * width;
return length + 1;
}
public static void Main(string[] args)
{
int a = 10;
int b = 5;
int r;
int c=rectangle(a, b, out r);
Console.WriteLine("矩形长为"+a);
Console.WriteLine("矩形宽为"+ b);
Console.WriteLine( "面积为" + r+"返回值"+c);
}
static void Square(params int[] s)
{
foreach (int g in s)
{
int radius = g;
int square = radius * radius;
Console.WriteLine(square);
}
}
public static void Main(string[] args)
{
int[] arr = { 1, 2, 3, 4 };
Square(arr);
}
练习题
static void Max(int x,int y, out int max)
{
if (x > y) max = x;
else max = y;
Console.WriteLine("max="+max);
}
public static void Main(string[] args)
{
int m;
Console.WriteLine("请输入a的值:");
int a = Convert.ToInt16(Console.ReadLine());
Console.WriteLine("请输入b的值:");
int b = Convert.ToInt16(Console.ReadLine());
Max(a, b, out m);
}
重载
static int Sum(int x,int y)
{
return x + y;
}
static float Sum(float x,float y)
{
return x+y;
}
static double Sum(int x,float y,double z){
return x+y+z;
}
public static void Main(string[] args)
{
int a = 10, b = 20;
float c = 10.5f, d = 34f;
double e = 40;
Console.WriteLine(Sum(a, b));
Console.WriteLine(Sum(c, d));
Console.WriteLine(Sum(a,c,e));
}
static double print(int i,double j){}
static double print(double i,int j){}
static void Main(string[] args)
{
double x=print(5,5);//二义性
}