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可能就不知道其具体的作用域是哪些范围 ...
随机推荐
- 小米2s换了屏幕后不能近距离对焦,拆过后无法对焦?
主要问题就是小米2/2S手机中壳套后摄像头的位置,里面还有一个正方形的黑色塑胶垫片,一般拆机后这个垫片是不会掉出来的,所以一般上盖时也是直接把中壳合上后上螺丝. 这样安装基本会导致塑胶垫片把摄像头顶住 ...
- 就要从SDG离职了
在知乎上看到有个提问,你为什么从盛大离职.我八月份在盛大游戏实习之间,下个星期准备辞职迎接新的挑战.本文也将提到我在盛大实习的经历以及离职的原因.当然,不会涉及很多SDG内部的管理以及技术上的秘密. ...
- phpcms v9二级栏目生成到根目录后三级栏目无法访问的解决办法
这个栏目问题折腾了我一天多,可怜我这个美工又不会程序!!! 我的问题是: 我的栏目设置如下: 一级栏目[生成到根目录(是)]> 二级栏目[生成到根目录(是)] > 三级栏目[生成到根目录( ...
- PHP数组的操作
一.数组操作的基本函数数组的键名和值array_values($arr);获得数组的值array_keys($arr);获得数组的键名array_flip($arr);数组中的值与键名互换(如果有重复 ...
- Keil V5.1x命令“Build Target”重新编译所有文件
网上的解决办法有多种,但不知道哪一种能对症,以下是我的解决方法:
- IOS设备滑动事件
只要手指触摸屏幕,滑动,从屏幕离开,系统都会产生UIEvent对象类型的事件---当然包括UITouch事件 – touchesBegan:withEvent: 当用户触摸到屏幕时调用方法 – t ...
- mysql子查询优化
,,,) ) LIMIT 第一种方式in where:2000ms SELECT COUNT(*) AS tp_count FROM xxx_b2c_orders o ,,,) and from xx ...
- easyui使用时出现这个Uncaught TypeError: Cannot read property 'combo' of undefined
easyui使用时出现这个Uncaught TypeError: Cannot read property 'nodeName' of undefined 最后检查发现是必须给select一个id,光 ...
- python学习笔记2(变量)
变量是只不过保留的内存位置用来存储值,这意味着,当创建一个变量,那么它在内存中保留一些空间. Python是弱类型,无需明确指定变量类型,赋值的同时会自动声明类型. x = 1 y = 2.0 nam ...
- 解决服务器断电导致mysql数据库无法启动
1.找到mysql数据库目录下的*.ini这个配置文件2.复制配置文件到桌面,当作备份3.打开配置文件,在配置文件里加上innodb_force_recovery=6这句,保存,然后重启服务器4重启完 ...