c# - 方法能否在运行时确定参数是否已默认?
问题描述
鉴于此示例代码,方法foo是否可以在运行时确定第 4 个参数是默认的?
using System;
namespace DefaultParameters {
class Program {
static void Main(string[] args) {
int a = 0,b = 1,c = 3;
foo(a, b, c); // The 4th parm is defaulted in the method definition.
foo(a, b, c, 100); // The 4th parm is explicit in the method call.
}
/// <summary>
/// A method with a default paramater
/// </summary>
/// <param name="speed">Speed</param>
/// <param name="brightness">Brightness</param>
/// <param name="weight">Weight</param>
/// <param name="humidity">Humidity</param>
public static void foo(int speed, int brightness, int weight, int humidity = 42) {
Console.WriteLine("speed = " + speed + " brightness = " + brightness + " weight = " + weight + " humidity = " + humidity);
}
}
}
解决方案
不
您可以随意猜测,如果它是默认值,则可能没有给出,但没有办法确定。
推荐阅读
- python - 在 Python 中提示输入时,如何允许用户输入多行?当按下 Enter 或在输入中使用 /n 时,它不起作用
- encoding - 西班牙文“ñ”eñe 0xa4和大写eñe 0xa5采用什么编码?
- javascript - 在 CefSharp 获得焦点之前加载页面
- r - 如何处理R中数据框中字符类型的空记录?
- javascript - 如何将按钮更改为可点击的图像
- python - Counting down lives in python turtle
- arrays - 将一个数组分成两个表格视图单元格
- github - github 页面无法将表中的 .md 转换为 .html
- python - 如何根据 Python 中每个字符串内的浮点数对列表进行排序?
- python - 检测鼠标悬停并在鼠标悬停时发出声音?