首页 新闻 会员 周边

如何将两行C#代码变成一行

1
悬赏园豆:100 [已关闭问题] 解决于 2012-06-13 13:31

两行代码如下:

BlogPost post = _postRepository.GetById(contentId);
return post == null ? default(Guid) : post.UserId;

两行代码的另一种实现:

BlogPost post;
return  (post = _postRepository.GetById(contentId)) == null? default(Guid) : post.UserId;

想要达到的一行代码效果(下面的代码只是示例,不能通过编译):

return  (BlogPost post = _postRepository.GetById(contentId)) == null? default(Guid) : postUserId;
C#
问题补充:

如果用Javascript,可以实现:

return (post = GetById(1)) == null ? "" : post.UserId;
dudu的主页 dudu | 高人七级 | 园豆:31003
提问于:2012-06-13 11:59
< >
分享
其他回答(3)
0

return _postRepository.GetById(contentId) == null? default(Guid) :_postRepository.GetById(contentId).UserId;

我只会这样。。。。

松鼠鱼 | 园豆:185 (初学一级) | 2012-06-13 12:05

这样会调用两次_postRepository.GetById(contentId)

支持(0) 反对(0) dudu | 园豆:31003 (高人七级) | 2012-06-13 12:06

@dudu: 知道会这么说的>_<

支持(0) 反对(0) 松鼠鱼 | 园豆:185 (初学一级) | 2012-06-13 12:21

@松鼠鱼: 

这样写方法的返回签名都变了。所以估计更不能够算了。

支持(0) 反对(0) ````` | 园豆:14268 (专家六级) | 2012-06-13 12:31

@dudu: 

return new Func<BlogPost, GUID>((b) => { return (b=_postRepository.GetById(contentId)) == null? default(Guid) : post.BlogSite.UCUserID.Value;}).Invoke(new BlogPost());

改了一下。。。刚刚少invoke了  此外假设返回的是GUID类型

支持(0) 反对(0) 松鼠鱼 | 园豆:185 (初学一级) | 2012-06-13 12:35

@松鼠鱼: 

View Code
return (_postRepository.GetById(contentId) ?? new BlogPost(UserId= default(Guid))).UserId;

貌似这样写就成。没做测试。。

支持(0) 反对(0) ````` | 园豆:14268 (专家六级) | 2012-06-13 12:38

@imfunny: 

哎呀,的确貌似。。。。??运算符从没用过,不过看上去的确是对的

支持(0) 反对(0) 松鼠鱼 | 园豆:185 (初学一级) | 2012-06-13 12:42
0
return  _postRepository.GetById(contentId) == null ? default(Guid) : _postRepository.GetById(contentId) .UserId;
土豆屋 | 园豆:354 (菜鸟二级) | 2012-06-13 12:06
0
return (_postRepository.GetById(contentId) ?? new BlogPost(UserId= default(Guid))).UserId;
这个样子估计编译会有问题,
_postRepository.GetById(contentId) 这个是实体post

BlogPost(UserId= default(Guid))).UserId 这个是实体的属性

Alvin | 园豆:828 (小虾三级) | 2012-06-13 12:48

实际上不是这样解析的。

(_postRepository.GetById(contentId) ?? new BlogPost(UserId= default(Guid))返回了一个不能够为空的属性。

如果为空了就赋默认值。

然后把UserId返回。

支持(0) 反对(0) ````` | 园豆:14268 (专家六级) | 2012-06-13 12:52

@imfunny:

貌似有点靠谱,少看了一个

支持(0) 反对(0) Alvin | 园豆:828 (小虾三级) | 2012-06-13 13:34
View Code
namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {

            Person c=new Person();
            c.Name="123";
            Console.Write(c.Name+Environment.NewLine);
            c = null;
            Console.Write((c?? new Person()).Name);
            Console.ReadKey();
        }


        public class Person
        {
            private int _id;
            private string _name="456";
            public Person()
            {
            }
            public int Id
            {
                get { return _id; }
                set { _id = value; }
            }
            public string Name
            {
                get { return _name; }
                set { _name = value; }
            }
        }
    }
}

随手写了个console。的确如此。

支持(0) 反对(0) Alvin | 园豆:828 (小虾三级) | 2012-06-13 13:42
0
return _postRepository.GetById(contentId)UserId ?? default(Guid);
淘@淘 | 园豆:602 (小虾三级) | 2012-06-13 13:13
return _postRepository.GetById(contentId).UserId ?? default(Guid);

刚才少写了一个点

支持(0) 反对(0) 淘@淘 | 园豆:602 (小虾三级) | 2012-06-13 13:14

@淘@淘: 

在_postRepository.GetById(contentId)返回null的时候会抛出未将对象引用到实例的。。

支持(0) 反对(0) ````` | 园豆:14268 (专家六级) | 2012-06-13 13:17
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册