default parameter value for ‘color’ must be a compile-time constant
定义了一个函数,函数有一个参数是Color类型的可选参数,想要设置其默认值为Color.Black
I've run into this as well and the only workaround I've found is to use nullables.
public void log(String msg, Color? c = null)
{
loggerText.ForeColor = c ?? Color.Black;
loggerText.AppendText("\n" + msg);
}
这个解决方法中的 ??https://msdn.microsoft.com/en-us/library/ms173224.aspx
The ?? operator is called the null-coalescing operator.
It returns the left-hand operand if the operand is not null;//操作符左边不为null的时候,取左边的值
otherwise it returns the right hand operand. //操作符左边为null的时候,取右边的值
http://stackoverflow.com/questions/5381717/defining-colors-as-constants-in-c-sharp
Only literals can be defined as const.
The difference is, that const values are hard-bakened into the assemblies that uses it. Should their definition change, then the call sites doesn't notice unless they get recompiled.
In contrast, readonly declares a variable in a way that it cannot be reassigned after outside theconstructor (or static constructor in case of a static readonly variable).
So, you have no other way then to use readonly here, since Color is a struct, and no primitive data type or literal.
default parameter value for ‘color’ must be a compile-time constant的更多相关文章
- Default Parameter Values in Python
Python’s handling of default parameter values is one of a few things that tends to trip up most new ...
- 《理解 ES6》阅读整理:函数(Functions)(一)Default Parameter Values
对于任何语言来说,函数都是一个重要的组成部分.在ES6以前,从JavaScript被创建以来,函数一直没有大的改动,留下了一堆的问题和很微妙的行为,导致在JavaScript中使用函数时很容易出现错误 ...
- python's default parameter
[python's default parameter] 对于值类型(int.double)的default函数参数,函数不会保存对默认类型的修改.对于mutable objectd类型的默认参数,会 ...
- 除去Scala的糖衣(13) -- Default Parameter Value
欢迎关注我的新博客地址:http://cuipengfei.me/ 好久没有写博客了,上一次更新竟然是一月份. 说工作忙都是借口,咋有空看美剧呢. 这半年荒废掉博客说到底就是懒,惯性的懒惰.写博客这事 ...
- JavaScript函数的默认参数(default parameter)
JavaScript函数的默认参数(default parameter) js函数参数的默认值都是undefined, ES5里,不支持直接在形参里写默认值.所以,要设置默认值,就要检测参数是否为un ...
- [Python] Pitfalls: About Default Parameter Values in Functions
Today an interesting bug (pitfall) is found when I was trying debug someone's code. There is a funct ...
- 条款37:绝不重新定义继承而来的缺省参数值(Never redefine a function's inherited default parameter value)
NOTE: 1.绝不重新定义一个继承而来的缺省参数值,因为缺省参数值都是静态绑定的,而virtual 函数-----你唯一应该覆盖的东西----却是动态绑定的.
- SSIS Parameter用法
今天学习SSISParameter的用法,记录学习的过程. Parameters能够在Project Deployment Model下使用,不能在Package Deployment Model使用 ...
- Java关键字(一) 修饰符private、protected、public和default的作用域
我们经常用着四种修饰符去修饰变量.方法和类,但是这四种的作用域都一样吗? 其中private和public可能是最多人知道的,但是protected和default可能就不知道其具体的作用域是哪些范围 ...
随机推荐
- Android sqlite
转载 http://blog.csdn.net/s874154731/article/details/7086238 import android.content.Context; import an ...
- NoSQL专家王涛访谈:为什么我们还要做一个NoSQL?
ChinaUnix:各位网友大家好,今天有幸请到王涛先生到CU做客,与大家交流一些工作经验.首先请王涛先介绍一下自己. 王涛:大家好,我是王涛.过去八年里我一直在IBM多伦多实验室从事DB2引擎研发的 ...
- 用CSS3写的小案例-图片缩放隐藏内容显示
思路分析 (1).搭建界面 (2).鼠标移到图片的时候,放大显示 (3).鼠标移入到当前的li标签里面找到后面的div让其显示出来 <!DOCTYPE html> <html lan ...
- PHP学习之环境搭建
计算机环境 win7 64位 搭建 apache-httpd-2.2-win64 + php-5.3.6-Win32-VC9-x64 +MySQL_5.5.13_winx64开发环境 参考: ...
- gets--vs--fgets
gets fget的区别 这两个函数都能从标准的输入流中读取信息,比如从键盘中输入信息,但是有些区别. 使用gets当输入的字符多于预定个数时候,会造成溢出,程序报错. int main(int ...
- HttpClient使用笔记
使用版本为4.5.1 常用API: 1.获取网页内容:InputStream in = response.getEntity().getContent() 2.获取状态码:response.getSt ...
- discuz!X2.5技术文档
discuz!系统常量: DISCUZ_ROOT //网站根目录 TIMESTAMP //程序执行的时间戳 CHARSET //程序的编码类型 FORMHASH //HASH值 其余 ...
- Oracle和Redhat下载地址
Oracle RedHat 用户名:zhangwei900808@126.com 密码:@XxxxxXxxxxxx 有网友想要的,请在留言板给我留言,我会把用户名和密码发给你!
- python 读写 Excel文件
最近用python处理一个小项目,其中涉及到对excel的读写操作,通过查资料及实践做了一下总结,以便以后用. python读写excel文件要用到两个库:xlrd和xlwt,首先下载安装这两个库. ...
- centos startx 进入桌面后使用中文输入法
(从runlevel 3进入桌面:英文 ,结果用不了中文输入法 囧) 但在中文桌面下可以输入中文 步骤: 由startx可以进入桌面 查看:which startx => /usr/bin/st ...