C# : 'is' keyword and checking for Not

后端 未结 12 1988
没有蜡笔的小新
没有蜡笔的小新 2020-12-02 05:23

This is a silly question, but you can use this code to check if something is a particular type...

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

Is ther

相关标签:
12条回答
  • 2020-12-02 05:48

    Why not just use the else ?

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

    Its neat it familiar and simple ?

    0 讨论(0)
  • 2020-12-02 05:49

    The extension method IsNot<T> is a nice way to extend the syntax. Keep in mind

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

    performs better than doing something like

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

    In your case, it doesn't matter as you are returning from the method. In other words, be careful to not do both the check for type and then the type conversion immediately after.

    0 讨论(0)
  • 2020-12-02 05:51

    The way you have it is fine but you could create a set of extension methods to make "a more elegant way to check for the 'NOT' instance."

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

    Then you could write:

    if (child.IsNot<IContainer>())
    {
        // child is not an IContainer
    }
    
    0 讨论(0)
  • 2020-12-02 05:54

    C# 9 (released with .NET 5) includes the logical patterns and, or and not, which allows us to write this more elegantly:

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

    Likewise, this pattern can be used to check for null:

    if (child is not null) { ... }
    
    0 讨论(0)
  • 2020-12-02 05:56
    if(!(child is IContainer))
    

    is the only operator to go (there's no IsNot operator).

    You can build an extension method that does it:

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

    and then use it to:

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

    And you could follow on your theme:

    public static bool IsNotAFreaking<T>(this object obj) {
        return !(obj is T);
    }
    
    if (child.IsNotAFreaking<IContainer>()) { // ...
    

    Update (considering the OP's code snippet):

    Since you're actually casting the value afterward, you could just use as instead:

    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...
    
    0 讨论(0)
  • 2020-12-02 06:04

    You can do it this way:

    object a = new StreamWriter("c:\\temp\\test.txt");
    
    if (a is TextReader == false)
    {
       Console.WriteLine("failed");
    }
    
    0 讨论(0)
提交回复
热议问题