当Debug.Assert()
源代码中存在方法调用并且我在发布模式下编译时,编译器是否会生成IL,Debug.Assert()
即使它没有被调用?
我们的一位开发人员最近添加了一个Assert,显示有关内部安全性的信息.有人可以查看发布模式IL并找出断言的文本吗?
我有一个IEnumerable <双>我要转换为一个IEnumerable <字符串>. 问题是下面的代码在select语句上抛出一个参数null异常. 我究竟做错了什么?
当我尝试迭代返回的IEnumerable <string>时,会发生实际问题.我得到一个InvalidCastException.我在debuger中看到strings = {System.Linq.Enumerable.WhereSelectEnumerableIterator <double,string>}
private IEnumerable<string> ConvertToString(IEnumerable<double> doubles)
{
IEnumerable<string> strings = null;
if (doubles != null)
strings = doubles.Select(d => ConvertToString(d));
return strings;
}
private string ConvertToString(double d)
{
return string.Format("{0:0.00}", d);
}
Run Code Online (Sandbox Code Playgroud)
好的,我解决了我的问题.这个Linq延迟执行使调试变得棘手.我实际上有一个呼叫上游导致问题.
ICollection<float> floats; //pretend it holds valid data
ConvertToString(floats.Cast<double>()) //<---This is naughty
Run Code Online (Sandbox Code Playgroud) 根据System.Enum类的MSDN文档的最佳实践部分:
不要仅仅为了枚举枚举本身的状态来定义枚举值.例如,不要定义仅标记枚举结束的枚举常量.如果需要确定枚举的最后一个值,请明确检查该值.此外,如果范围内的所有值都有效,则可以对第一个和最后一个枚举常量执行范围检查.
如果我理解正确,我们不应该如下声明枚举.
public enum DrawOrder
{
VeryBottom = 0,
Bottom = 1,
Middle = 2,
Top = 3,
Lowest = VeryBottom, //marks a position in the enum
Highest = Top, //marks a position in the enum
}
Run Code Online (Sandbox Code Playgroud)
为什么这被认为是不好的做法?
我有一个IEnumerable<Point>
,我想将其转换为IEnumerable<float>
.
我可以在单个Linq语句中执行此操作吗?
IEnumerable<float> ConvertToPoints(IEnumerable<Point> points)
{
List<float> floats = new List<float>();
foreach(var point in points)
{
floats.Add(point.X);
floats.Add(point.Y);
}
return floats;
}
Run Code Online (Sandbox Code Playgroud)