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可能就不知道其具体的作用域是哪些范围 ...
随机推荐
- TweenMax动画库学习(四)
目录 TweenMax动画库学习(一) TweenMax动画库学习(二) TweenMax动画库学习(三) Tw ...
- linux find 反转 查找没有被找到的结果
在linux下,有时候需要找一些文件,还有时候这些文件格式不够统一和规范,但是需要排除的那些文件却格式统一,就可以使用find命令的反转功能 一般用find查找文件的命令是: find . -name ...
- [原创] linux课堂-学习笔记-目录及概况
本学习笔记基于:网易云课堂-linux课堂 课时1Centos 6.4安装讲解46:14 课时2Centos 6.4桌面环境介绍与网络连接04:30 课时3 Linux目录结构介绍及内核与shell分 ...
- Qt Creator (C++)保存文件
最近在学习QT Creator,感觉很是头大.可能是刚刚学的原因吧,感觉完全没有C#好,好多东西完全搞不懂. C++虽然很灵活,但是也可能是太灵活了,总是搞得人一头雾水. 一个简简单单的保存文件,就让 ...
- php常量运用注意
多个文件引入,常量重复定义会失效,后者无法重复定义...靠
- PHP 跨域写cookie
实际工作中,类似这样的要求很多,比如说,我们有两个域名,我们想实现在一个域名登录后,能自动完成另一个域名的登录,也就是PASSPORT的功能. 我只写一个大概,为了测试的方便,先编辑hosts文件,加 ...
- 浏览器兼容CSS代码:按钮文字垂直居中(input button text vertical align)
经过测试的浏览器:IE6, IE7, IE8, IE9, Firefox, Chrome, Safiri, Maxthon 按钮的HTML代码: <input id="btn_comm ...
- RTC搭建android下三层应用程序访问服务器MsSql-服务器端
前几天通过Ro搭建webservice,然后在android下调用,虽然已近成功,但是返回的数据库里的中文有乱码一直未得到解决!rtc6.23版本,已经支持xe5,也支持fmx的android下开发, ...
- AvalonDock 2.0 的简单运用
最近在研究AvalonDock的一些使用,碰到了一些问题.现在拿出来跟大家分享分享. 网上找了一大把AvalonDock 1.3版本的资料,弄出Demo后发现属性面板(DockableContent) ...
- Sql查询除ID以外相同的数据
id NAME AGE1 n1 12 n1 13 n2 24 n2 25 n22 ...