小编Fel*_*lan的帖子

构造函数或显式强制转换

在使用Linq to Sql时,我创建了一个单独的类来将数据传送到网页.为了简化创建这些渡轮对象,我要么使用专门的构造函数,要么使用显式转换运算符.我有两个问题.

从可读性的角度来看,哪种方法更好?

第二,虽然生成的clr代码对我来说似乎是相同的,但是在某些情况下编译器会处理一个不同于另一个的情况(在lambda等中).

示例代码(DatabaseFoo使用专门的构造函数,BusinessFoo使用显式运算符):

public class DatabaseFoo
{
    private static int idCounter; // just to help with generating data
    public int Id { get; set; }
    public string Name { get; set; }

    public DatabaseFoo()
    {
        Id = idCounter++;
        Name = string.Format("Test{0}", Id);
    }
    public DatabaseFoo(BusinessFoo foo)
    {
        this.Id = foo.Id;
        this.Name = foo.Name;
    }
}

public class BusinessFoo
{
    public int Id { get; set; }
    public string Name { get; set; }

    public static explicit operator …
Run Code Online (Sandbox Code Playgroud)

c# linq linq-to-sql

4
推荐指数
1
解决办法
3136
查看次数

标签 统计

c# ×1

linq ×1

linq-to-sql ×1