Method Parameter Modifiers (C#)

ModifierRemarks
(None)

value type: passed by value (a copy of data)

reference type: passed by reference

Note: string is by default passed by value.

outout parameters are passed by reference. Must be assigned in the method.
into indicates that the ref parameters are read-only for the method.
refpassed by reference. The method may modify the intial value.
paramsonly one params modifier is allowed in a method. It is a parameter with a variable number of arguments

 1. out parameters

out parameters could be discarded when not needed.

// long multi;
// long add;
// new in C# 7.0, out parameters could be directly declared inside the method call.
Calculator(2, 100, out long multi, out long add);
// Discarding out paramters (new 7.0) when not needed for better reading and perfomance.
Calculator(100, 200, out long multi2, out _);
Console.WriteLine($"The multiplication result is {multi}");
Console.WriteLine(($"the add result is {add}"));
Console.ReadLine();

static void Calculator(int x, int y, out long multi, out long add)
{
    multi = x * y;
    add = x + y;
}

2. ref parameters

int x = 100;
int y = 200;
Double(ref x, ref y);
Console.WriteLine($"The result of double is {x}, {y}");
Console.ReadLine();

static void Double(ref int x, ref int y)
{
    x = x * 2;
    y = y * 2;
}
// output:
// The result of double is 200, 400

 3. in paramters

The in paramters are passed by reference byut prevents the called method to modify the values (read-only).

int x = 100;
int y = 2;

Console.WriteLine($"The result of double is {Double(x, y)}");
Console.ReadLine();

static int Double(in int x, in int y)
{
    //x = x * 2; // CS8331: Cannot assign to variable 'in int' because it is readonly.
    //y = y * 2; // CS8331: Cannot assign to variable 'in int' because it is readonly.
    return x * y;
}

4. params paramters

// directly a list of doubles
var maximum1 = FindMaximum(1, 2, 40, 4);
// directly an array
double[] list = { 1, 20, 88, 102, 12 };
var maximum2 = FindMaximum(list);
Console.WriteLine($"The result is {maximum1}");
Console.WriteLine($"The result is {maximum2}");

Console.ReadLine();

static double FindMaximum(params double[] values)
{
    double maximum = 0;
    if (values.Length == 0) return maximum;
    maximum = values[0];
    for (int i = 1; i < values.Length; i++)
    {
        if (values[i] > maximum) maximum = values[i];
    }
    return maximum;
}

5. Optional Parameters and Named Arguments

The value for an optional paramter must be given at complie time. Error if given at runtime.

// using default optional value
int result1 = Double(10);
// specify an optional value
int result2 = Double(10, 10);
// using named arguments
int result3= Double(10, coefficient: 3);
Console.WriteLine($"The result is {result1}");
Console.WriteLine($"The result is {result2}");
Console.WriteLine($"The result is {result3}");

Console.ReadLine();

static int Double(int x, int coefficient = 2)
{
    int result = x * coefficient;
    return result;
}

6. Method Overloading

namespace ConsoleApp11
{
    public static class Calculation
    {
        public static int Calculator(int x, int y)
        {
            int result = x * y;
            return result;
        }

        public static double Calculator(double x, double y)
        {
            double result = y * x;
            return result;
        }

        public static long Calculator(long x, long y)
        {
            long result = x * y;
            return result;
        }
    }
}
using static ConsoleApp11.Calculation;

int result1 = Calculator(10, 10);
double result2 = Calculator(2.1, 2.3);

Console.WriteLine($"The result is {result1}");
Console.WriteLine($"The result is {result2}");

Console.ReadLine();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值