C# 简化

C# 简化,c#,C#,可能写得很简单,代码如下: foreach (var friend in friends) { friend.Value.blockQuote = GetBlockQuote(friend.Value.nick); if (friend.Value.photo == "0") { if (friend.Value.sex == 1) { var img = new BitmapImage();

可能写得很简单,代码如下:

foreach (var friend in friends)
{
    friend.Value.blockQuote = GetBlockQuote(friend.Value.nick);

    if (friend.Value.photo == "0")
    {
        if (friend.Value.sex == 1)
        {
            var img = new BitmapImage();
            img.BeginInit();
            img.UriSource = new Uri(@"avatars\man.jpg",
                                    UriKind.Relative);
            img.EndInit();
            friend.Value.profilePhoto = img;
        }
        if (friend.Value.sex == 2)
        {
            //da default
            var img = new BitmapImage();
            img.BeginInit();
            img.UriSource = new Uri(@"avatars\woman.jpg",
                                    UriKind.Relative);
            img.EndInit();
            friend.Value.profilePhoto = img;
        }
    }
    else
    {
        var img = new BitmapImage();
        img.BeginInit();
        img.UriSource = new Uri(friend.Value.photo.Replace(@"\", "").Replace(@"s_", ""), UriKind.Absolute);
        img.EndInit();
        friend.Value.profilePhoto = img;
    }
}

你可以算出这些线

var img = new BitmapImage();
img.BeginInit();


通过将它们放在
if
/
else
块的前面(对于前者)和后面(对于后者)。

您可以计算出这些行

var img = new BitmapImage();
img.BeginInit();


将它们放在
if
/
else
块之前(对于前者)和之后(对于后者)。

断开uri设置部分

foreach (var friend in friends)
{
    friend.Value.blockQuote = GetBlockQuote(friend.Value.nick);

    Uri uri;
    if (friend.Value.photo == "0")
    {
        if (friend.Value.sex == 1)
        {
            uri = new Uri(@"avatars\man.jpg", UriKind.Relative);
        }
        else if (friend.Value.sex == 2)
        {
            //da default
            uri = new Uri(@"avatars\woman.jpg", UriKind.Relative);
        }
        else
        {
            uri = null; // insert error handling here
        }
    }
    else
    {
        uri = new Uri(friend.Value.photo.Replace(@"\", "").Replace(@"s_", ""), UriKind.Absolute);
    }
    var img = new BitmapImage();
    img.BeginInit();
    img.UriSource = uri;
    img.EndInit();
    friend.Value.profilePhoto = img;
}
编辑

注意,if-else部分现在是重构->提取方法的一个很好的候选部分

foreach (var friend in friends)
{
    friend.Value.blockQuote = GetBlockQuote(friend.Value.nick);

    Uri uri;
    if (friend.Value.photo == "0")
    {
        if (friend.Value.sex == 1)
        {
            uri = new Uri(@"avatars\man.jpg", UriKind.Relative);
        }
        else if (friend.Value.sex == 2)
        {
            //da default
            uri = new Uri(@"avatars\woman.jpg", UriKind.Relative);
        }
        else
        {
            uri = null; // insert error handling here
        }
    }
    else
    {
        uri = new Uri(friend.Value.photo.Replace(@"\", "").Replace(@"s_", ""), UriKind.Absolute);
    }
    var img = new BitmapImage();
    img.BeginInit();
    img.UriSource = uri;
    img.EndInit();
    friend.Value.profilePhoto = img;
}
foreach (var friend in friends)
{
    friend.Value.blockQuote = GetBlockQuote(friend.Value.nick);
    var img = new BitmapImage();
    img.BeginInit();

    if (friend.Value.photo == "0")
    {
        if (friend.Value.sex == 1)
        {
             img.UriSource = new Uri(@"avatars\man.jpg",
        }
        if (friend.Value.sex == 2)
        {
             img.UriSource = new Uri(@"avatars\woman.jpg",
                                                UriKind.Relative);
        }
     }
     else
     {
          img.UriSource = new Uri(friend.Value.photo.Replace(@"\", "").Replace(@"s_", ""), UriKind.Absolute);

     }

     img.EndInit();
     friend.Value.profilePhoto = img;
}
编辑

请注意,if-else部分现在是重构->提取方法的一个很好的候选者,这是一个很好的答案,但有一个小缺点:这可能会给您一个未初始化的变量错误,因为
uri
没有明确指定。如果
friend.Value.sex
不是1或2会怎么样?@Jim Mischel已经发现并更新了示例。@Jim Mischel,这就是为什么我在声明时没有初始化
uri
变量来捕捉这些错误的原因,但我的头并没有像编译器那样工作……对Jim Mischel来说,friend.Value.sex可以是1或2。没有其他值。@Markus:假设是导致错误缠身的代码的路径。如果它只能是两个值中的一个,请使用
enum
类型而不是整数。答案很好,但有一个小缺点:这可能会给您一个未初始化的变量错误,因为
uri
没有明确指定。如果
friend.Value.sex
不是1或2会怎么样?@Jim Mischel已经发现并更新了示例。@Jim Mischel,这就是为什么我在声明时没有初始化
uri
变量来捕捉这些错误的原因,但我的头并没有像编译器那样工作……对Jim Mischel来说,friend.Value.sex可以是1或2。没有其他值。@Markus:假设是导致错误缠身的代码的路径。如果只能是两个值中的一个,请使用
enum
类型而不是整数。
foreach (var friend in friends)
{
    friend.Value.blockQuote = GetBlockQuote(friend.Value.nick);
    var img = new BitmapImage();
    img.BeginInit();

    if (friend.Value.photo == "0")
    {
        if (friend.Value.sex == 1)
        {
             img.UriSource = new Uri(@"avatars\man.jpg",
        }
        if (friend.Value.sex == 2)
        {
             img.UriSource = new Uri(@"avatars\woman.jpg",
                                                UriKind.Relative);
        }
     }
     else
     {
          img.UriSource = new Uri(friend.Value.photo.Replace(@"\", "").Replace(@"s_", ""), UriKind.Absolute);

     }

     img.EndInit();
     friend.Value.profilePhoto = img;
}