C# - 分配在if语句 [英] C# - Assignment in an if statement

查看:160
本文介绍了C# - 分配在if语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类动物和它的子类
我经常发现自己的编码以下行:

 如果(动物是狗)
{
狗狗=动物如狗;
dog.Name;

}

有关变量 。兽兽;



有一些语法,让我写的东西,如:

 如果(狗狗=动物如狗)
{
dog.Name;

}


解决方案

不,没有。这是更地道写这个虽然:

 狗狗=动物如狗; 
如果(狗!= NULL)
{
//使用狗
}

鉴于作为随后如果是几乎<青霉>总是的使用这种方法,可能会更有意义为了有其在一个去执行两个部分的操作。这是目前未在C#6,而可以是C#的7部,如果模式匹配提案的实施



的问题是,你不能的声明的中的一个条件部分的变量如果语句 1 。我能想到的最接近是这样的:

  //恶恶恶的。不使用。 
为(狗狗=动物如狗;!狗= NULL;狗= NULL)
{

}

这只是讨厌的...(我刚刚试了一下,它的工作。但是,请请不要'做T这一点。哦,你可以声明当然使用 VAR



当然,你可以写一个扩展方法:

 公共静态无效阿西夫< T>(本对象的值,动作< T>动作),其中T:类
{
T(T)=价值为T;
如果(T!= NULL)
{
动作(T);
}
}



然后调用它:

  animal.AsIf<狗和GT;(狗= GT; {
//使用狗在这里
});



另外,您也可以将二者结合起来:

 公共静态无效阿西夫< T>(此对象的值,动作< T>动作),其中T:类
{
//恶恶EVIL
。对于(VAR T =价值为T; T!= NULL; T = NULL)
{
动作(T);
}
}

您也可以使用扩展方法没有lambda表达式在比更清洁的方式for循环:

 公共静态的IEnumerable< T> AsOrEmpty(该对象的值)
{
T(T)=价值为T;
如果(T!= NULL)
{
收益率的回报吨;
}
}



然后:

 的foreach(狗狗在animal.AsOrEmpty<狗>())
{
//使用狗
}






1 可以的如果语句中指定 的值,但我很少这样做。这是不一样的,虽然声明变量。这不是的非常的不寻常我读数据流时,做在,而虽然。例如:

 串线; 
,而((行= reader.ReadLine())!= NULL)
{

}

这些天,我通常喜欢使用的包装,它可以让我用的foreach(以串线......)但我认为上面的一个漂亮的惯用模式。它的一般的不是很好有一个条件中的副作用,而且替代品通常涉及重复代码,当你知道这种模式很容易得到正确的。


I have a class Animal, and its subclass Dog. I often find myself coding the following lines:

if (animal is Dog)
{
    Dog dog = animal as Dog;    
    dog.Name;    
    ... 
}

For the variable Animal animal;.

Is there some syntax that allows me to write something like:

if (Dog dog = animal as Dog)
{    
    dog.Name;    
    ... 
}

解决方案

No, there isn't. It's more idiomatic to write this though:

Dog dog = animal as Dog;
if (dog != null)
{
    // Use dog
}

Given that "as followed by if" is almost always used this way, it might make more sense for there to be an operator which performs both parts in one go. This isn't currently in C# 6, but may be part of C# 7, if the pattern matching proposal is implemented.

The problem is that you can't declare a variable in the condition part of an if statement1. The closest approach I can think of is this:

// EVIL EVIL EVIL. DO NOT USE.
for (Dog dog = animal as Dog; dog != null; dog = null)
{
    ...
}

That's just nasty... (I've just tried it, and it does work. But please, please don't do this. Oh, and you can declare dog using var of course.)

Of course you could write an extension method:

public static void AsIf<T>(this object value, Action<T> action) where T : class
{
    T t = value as T;
    if (t != null)
    {
        action(t);
    }
}

Then call it with:

animal.AsIf<Dog>(dog => {
    // Use dog in here
});

Alternatively, you could combine the two:

public static void AsIf<T>(this object value, Action<T> action) where T : class
{
    // EVIL EVIL EVIL
    for (var t = value as T; t != null; t = null)
    {
        action(t);
    }
}

You can also use an extension method without a lambda expression in a cleaner way than the for loop:

public static IEnumerable<T> AsOrEmpty(this object value)
{
    T t = value as T;
    if (t != null)
    {
        yield return t;
    }
}

Then:

foreach (Dog dog in animal.AsOrEmpty<Dog>())
{
    // use dog
}


1 You can assign values in if statements, although I rarely do so. That's not the same as declaring variables though. It's not terribly unusual for me to do it in a while though when reading streams of data. For example:

string line;
while ((line = reader.ReadLine()) != null)
{
    ...
}

These days I normally prefer to use a wrapper which lets me use foreach (string line in ...) but I view the above as a pretty idiomatic pattern. It's usually not nice to have side-effects within a condition, but the alternatives usually involve code duplication, and when you know this pattern it's easy to get right.

这篇关于C# - 分配在if语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆