我最近正在使用一个DateTime对象,并写了这样的东西:
DateTime dt = DateTime.Now;
dt.AddDays(1);
return dt; // still today's date! WTF?
AddDays()的智能感知文档说它在日期后添加了一天,但它并没有这样做——它实际上返回了一个添加了一天的日期,所以你必须这样写:
DateTime dt = DateTime.Now;
dt = dt.AddDays(1);
return dt; // tomorrow's date
这个问题以前已经困扰过我很多次了,所以我认为将最糟糕的c#陷阱分类会很有用。
抛出收到异常
一个让许多新开发人员困惑的问题是重新抛出异常语义。
我经常看到如下代码
catch(Exception e)
{
// Do stuff
throw e;
}
问题是它会清除堆栈跟踪,并使诊断问题变得更加困难,因为您无法跟踪异常起源于何处。
正确的代码是不带参数的throw语句:
catch(Exception)
{
throw;
}
或者将异常包装在另一个异常中,并使用内部异常获取原始堆栈跟踪:
catch(Exception e)
{
// Do stuff
throw new MySpecialException(e);
}
重载==操作符和非类型化容器(数组列表、数据集等):
string my = "my ";
Debug.Assert(my+"string" == "my string"); //true
var a = new ArrayList();
a.Add(my+"string");
a.Add("my string");
// uses ==(object) instead of ==(string)
Debug.Assert(a[1] == "my string"); // true, due to interning magic
Debug.Assert(a[0] == "my string"); // false
解决方案?
总是使用字符串。当比较字符串类型时等于(a, b)
使用像List<string>这样的泛型来确保两个操作数都是字符串。
Linq2SQL:接口成员的映射[…]不支持。
如果对实现接口的对象执行Linq2Sql查询,则会得到非常奇怪的行为。假设你有一个类MyClass,它实现了一个接口IHasDescription,这样:
public interface IHasDescription {
string Description { get; set; }
}
public partial class MyClass : IHasDescription { }
(MyClass的另一半是一个Linq2Sql生成的类,包括属性Description。)
现在你写一些代码(这通常发生在泛型方法中):
public static T GetByDescription<T>(System.Data.Linq.Table<T> table, string desc)
where T : class, IHasDescription {
return table.Where(t => t.Description == desc).FirstOrDefault();
}
编译正常-但你会得到一个运行时错误:
NotSupportedException: The mapping of interface member IHasDescription.Description is not supported.
现在该怎么办呢?好吧,这真的很明显:只需将==更改为.Equals(),这样:
return table.Where(t => t.Description.Equals(desc)).FirstOrDefault();
现在一切都好了!
在这里看到的。