C#方法参数用法完整详细指南

2021年5月8日16:34:54 发表评论 797 次浏览

C#中的方法通常是程序中的代码块或语句,它让用户能够重用相同的代码,从而最终节省了过多的内存使用,起到节省时间的作用,更重要的是,它提供了更好的代码可读性。因此,可以说方法是执行某些特定任务的语句的集合,并且可能/可能不会将结果返回给调用者。

在某些情况下,用户可能希望执行某个方法,但有时该方法需要一些有价值的输入以执行和完成其任务。这些输入值在计算机语言术语中称为参数。

C#包含以下类型的方法参数

命名参数

参考参数

输出参数

默认或可选参数

动态参数

值参数

参数

命名参数

使用命名参数, 可以根据参数名称而不是方法中的顺序指定参数的值。换句话说, 它为我们提供了一种不记住参数顺序的便利。这个概念是在C#4.0中引入。当你在方法中使用大量参数时, 它使你的程序更易于理解。但是请记住, 命名参数总是出现在固定参数之后, 如果尝试在命名参数之后提供固定参数, 则编译器将抛出错误。

例子:

//C# program to illustrate the 
//concept of the named parameters
using System;
  
public class GFG {
  
     //addstr contain three parameters
     public static void addstr( string s1, string s2, string s3)
     {
         string result = s1 + s2 + s3;
         Console.WriteLine( "Final string is: " + result);
     }
  
     //Main Method
     static public void Main()
     {
         //calling the static method with named 
         //parameters without any order
         addstr(s1: "Geeks" , s2: "for" , s3: "Geeks" );
                     
     }
}

输出如下:

Final string is: lsbin

参考参数

ref是C#中的关键字, 用于通过引用传递值类型。或者我们可以说, 当控件返回到调用方法时, 如果对方法中的此参数进行的任何更改都将反映在该变量中。 ref参数不传递该属性。在ref参数中, 有必要在传递给ref之前对参数进行初始化。当被调用的方法还需要更改所传递参数的值时, 通过ref参数传递值很有用。

例子:

//C# program to illustrate the
//concept of ref parameter
using System;
  
class GFG {
  
     //Main Method
     public static void Main()
     {
  
         //Assigning value
         string val = "Dog" ;
  
         //Pass as a reference parameter
         CompareValue( ref val);
  
         //Display the given value
         Console.WriteLine(val);
     }
  
     static void CompareValue( ref string val1)
     {
         //Compare the value
         if (val1 == "Dog" ) 
         {
             Console.WriteLine( "Matched!" );
         }
  
         //Assigning new value
         val1 = "Cat" ;
     }
}

输出如下:

Matched!
Cat

输出参数

out是C#中的关键字, 用于将参数作为引用类型传递给方法。通常在方法返回多个值时使用。 out参数不会传递该属性。传递给输出之前, 无需初始化参数。当方法返回多个值时, 整个参数的参数声明非常有用。

例子:

//C# program to illustrate the
//concept of out parameter
using System;
  
class GFG {
  
     //Main method
     static public void Main()
     {
  
         //Creating variable
         //without assigning value
         int num;
  
         //Pass variable num to the method
         //using out keyword
         AddNum( out num);
  
         //Display the value of num
         Console.WriteLine( "The sum of"
           + " the value is: {0}" , num);
                            
     }
  
     //Method in which out parameter is passed
     //and this method returns the value of
     //the passed parameter
     public static void AddNum( out int num)
     {
         num = 40;
         num += num;
     }
}

输出如下:

The sum of the value is: 80

默认或可选参数

顾名思义, 可选参数不是强制性参数, 它们是可选参数。它有助于排除某些参数的参数。或者我们可以说在可选参数中, 没有必要在方法中传递所有参数。这个概念是在C#4.0中引入。在这里, 每个可选参数都包含一个默认值, 这是其定义的一部分。如果我们没有将任何参数传递给可选参数, 那么它将采用其默认值。可选参数始终在参数列表的末尾定义。换句话说, 方法, 构造函数等的最后一个参数是可选参数。

例子:

//C# program to illustrate the 
//concept of optional parameters 
using System; 
    
class GFG { 
    
     //This method contains two regular 
     //parameters, i.e. ename and eid
     //And two optional parameters, i.e. 
     //bgrp and dept 
     static public void detail( string ename, int eid, string bgrp = "A+" , string dept = "Review-Team" ) 
    
     { 
         Console.WriteLine( "Employee name: {0}" , ename); 
         Console.WriteLine( "Employee ID: {0}" , eid); 
         Console.WriteLine( "Blood Group: {0}" , bgrp); 
         Console.WriteLine( "Department: {0}" , dept); 
     } 
    
     //Main Method 
     static public void Main() 
     { 
    
         //Calling the detail method 
         detail( "XYZ" , 123); 
         detail( "ABC" , 456, "B-" ); 
         detail( "DEF" , 789, "B+" , "Software Developer" ); 
     } 
}

输出如下:

Employee name: XYZ
Employee ID: 123
Blood Group: A+
Department: Review-Team
Employee name: ABC
Employee ID: 456
Blood Group: B-
Department: Review-Team
Employee name: DEF
Employee ID: 789
Blood Group: B+
Department: Software Developer

动态参数

InC#4.0, 引入了一种新型的参数, 称为动态参数。这里的参数是动态传递的, 这意味着编译器不会在编译时检查动态类型变量的类型, 而是由编译器在运行时获取类型。动态类型变量是使用dynamic关键字创建的。

例子:

//C# program to illustrate the concept 
//of the dynamic parameters
using System;
  
class GFG {
  
     //Method which contains dynamic parameter
     public static void mulval(dynamic val)
     {
         val *= val;
         Console.WriteLine(val);
     }
  
     //Main method
     static public void Main()
     {
  
         //Calling mulval method
         mulval(30);
     }
}

输出如下:

900

值参数

它是方法中的常规值参数, 或者你可以说按值传递值类型。因此, 当变量作为值类型传递时, 它们包含数据或值, 而不包含任何引用。如果你要对值类型参数进行任何更改, 那么它将不会反映作为参数存储的原始值。

例子:

//C# program to illustrate value parameters
using System;
  
public class GFG {
  
     //Main Method
     static public void Main()
     {
  
         //The value of the parameter
         //is already assigned
         string str1 = "Geeks" ;
         string str2 = "geeks" ;
         string res = addstr(str1, str2);
         Console.WriteLine(res);
     }
  
     public static string addstr( string s1, string s2)
     {
         return s1 + s2;
     }
}

输出如下:

Geeksgeeks

参数

当程序员对要使用的参数数量没有任何先验知识时, 这很有用。通过使用参数, 你可以传递任意数量的参数。在params关键字之后, 只允许使用一个params关键字, 并且在函数声明中将不允许其他Params。如果不传递任何参数, 则参数的长度将为零。

例子:

//C# program to illustrate params
using System;
namespace Examples {
  
class Geeks {
  
     //function containing params parameters
     public static int mulval( params int [] num)
     {
         int res = 1;
  
         //foreach loop
         foreach ( int j in num)
         {
             res *= j;
         }
         return res;
     }
  
     static void Main( string [] args)
     {
  
         //Calling mulval method
         int x = mulval(20, 49, 56, 69, 78);
  
         //show result
         Console.WriteLine(x);
     }
}
}

输出如下:

295364160

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: