316

这是一个愚蠢的问题,但是您可以使用此代码来检查某些东西是否是特定类型...

if (child is IContainer) { //....

有没有更优雅的方法来检查“NOT”实例?

if (!(child is IContainer)) { //A little ugly... silly, yes I know...

//these don't work :)
if (child !is IContainer) {
if (child isnt IContainer) { 
if (child aint IContainer) { 
if (child isnotafreaking IContainer) { 

是的,是的……愚蠢的问题……

因为对代码的外观有一些疑问,所以它只是在方法开头的简单返回。

public void Update(DocumentPart part) {
    part.Update();
    if (!(DocumentPart is IContainer)) { return; }
    foreach(DocumentPart child in ((IContainer)part).Children) {
       //...etc...
4

13 回答 13

324
if(!(child is IContainer))

是唯一的运营商(没有IsNot运营商)。

您可以构建一个扩展方法来完成它:

public static bool IsA<T>(this object obj) {
    return obj is T;
}

然后用它来:

if (!child.IsA<IContainer>())

你可以按照你的主题:

public static bool IsNotAFreaking<T>(this object obj) {
    return !(obj is T);
}

if (child.IsNotAFreaking<IContainer>()) { // ...

更新(考虑到 OP 的代码片段):

由于您实际上是在之后转换值,因此您可以as改用:

public void Update(DocumentPart part) {
    part.Update();
    IContainer containerPart = part as IContainer;
    if(containerPart == null) return;
    foreach(DocumentPart child in containerPart.Children) { // omit the cast.
       //...etc...
于 2009-05-01T14:35:46.980 回答
116

你可以这样做:

object a = new StreamWriter("c:\\temp\\test.txt");

if (a is TextReader == false)
{
   Console.WriteLine("failed");
}
于 2009-05-01T14:47:02.540 回答
45

C# 9.0 中的新功能

https://devblogs.microsoft.com/dotnet/welcome-to-c-9-0/#logical-patterns

if (part is not IContainer)
{
    return;
}

原始答案

这还没有被提及。它有效,我认为它看起来比使用更好!(child is IContainer)

if (part is IContainer is false)
{
    return;
}
于 2019-01-23T20:15:53.770 回答
16

C# 9(与 .NET 5 一起发布)包括逻辑模式and,ornot,这使我们可以更优雅地编写它:

if (child is not IContainer) { ... }

同样,此模式可用于检查 null:

if (child is not null) { ... }
于 2020-05-28T06:16:42.973 回答
10

为什么不只使用 else ?

if (child is IContainer)
{
  //
}
else
{
  // Do what you want here
}

它的整洁它熟悉和简单吗?

于 2009-05-01T14:38:01.220 回答
10

你拥有它的方式很好,但你可以创建一组扩展方法来制作“一种更优雅的方式来检查'NOT'实例。”

public static bool Is<T>(this object myObject)
{
    return (myObject is T);
}

public static bool IsNot<T>(this object myObject)
{
    return !(myObject is T);
}

然后你可以写:

if (child.IsNot<IContainer>())
{
    // child is not an IContainer
}
于 2009-05-01T14:45:38.987 回答
5

丑陋的?我不同意。唯一的另一种方式(我个人认为这是“丑陋”):

var obj = child as IContainer;
if(obj == null)
{
   //child "aint" IContainer
}
于 2009-05-01T14:38:45.523 回答
3

扩展方法IsNot<T>是扩展语法的好方法。记住

var container = child as IContainer;
if(container != null)
{
  // do something w/ contianer
}

比做类似的事情表现更好

if(child is IContainer)
{
  var container = child as IContainer;
  // do something w/ container
}

在您的情况下,您从该方法返回时并不重要。换句话说,注意不要同时检查类型,然后立即进行类型转换。

于 2009-05-01T16:34:49.643 回答
3

is运算符计算为布尔结果,因此您可以做任何您本来可以在布尔上做的事情。要否定它,请使用!运算符。您为什么要为此使用不同的运算符?

于 2009-05-01T14:41:59.743 回答
3

虽然这并不能避免括号的问题,但为了人们通过 Google 到达这里,应该提到存在更新的语法(从 C# 7 开始)以使其余代码更简洁:

if (!(DocumentPart is IContainer container)) { return; }
foreach(DocumentPart child in container.Children) {
    ...

这避免了双重转换、空检查以及在可能为空的范围内有一个可用的变量。

于 2018-03-07T16:12:02.190 回答
2

虽然 IS 运算符通常是最好的方法,但您可以在某些情况下使用另一种方法。您可以使用 as 运算符并测试是否为空。

MyClass mc = foo as MyClass;
if ( mc == null ) { }
else {}
于 2009-05-01T14:40:48.093 回答
-1

会用这个

如果(!(对象是汽车)){

}

于 2021-09-08T00:49:47.747 回答
-3
if (child is IContainer ? false : true)
于 2014-04-23T16:43:14.337 回答