What is the difference between typeof(), GetType() and IS in C# and VB.NET

What is the difference between typeof(), GetType() and is?

  • typeof() takes a type name specified at compile Time.
  • GetType() gets the Type of an Object at runtime
  • is returns true if an instance is in the inheritance tree. Since the is Keyword is casting the object, you should use it with care.
  • here is an example.

    class Employee { } 
    class Developer : Employee { }
    
    void PrintTypes(Employee e) { 
        print(e.GetType() == typeof(Employee)) // false 
        print(e is Employee)                   // true 
        print(e.GetType() == typeof(Developer))    // true
    }
    

    How to get all types that implement a specific Interface in C# and VB.NET

    To retrieve a IEnuemerable of all types in the current or another Assembly that implement a specific Interface / Abstract class and so on, you can use the snippets below.

    Methods

    C# Version

    VB.NET Version

    Compatibility: working .NET 2.0 working .NET 3.0 not tested .NET 3.5 not working .NET 4.0 not working .NET 4.5

    Do you have an alternate or better method for this task?
    If you have any questions or suggestions feel free to rate this snippet, post a comment or Contact Us via Email.

    Related links:

    What is the best datatype for money in C# and VB.NET

    The best Datatype for money in C# and VB.NET is Decimal.
    What is the MSDN saying about Decimal.

    The Decimal value type represents decimal numbers ranging from positive 79,228,162,514,264,337,593,543,950,335 to negative 79,228,162,514,264,337,593,543,950,335. The Decimal value type is appropriate for financial calculations requiring large numbers of significant integral and fractional digits and no round-off errors. The Decimal type does not eliminate the need for rounding. Rather, it minimizes errors due to rounding.

    MSDN SOURCE

    How to scroll to the end of a TreeView in C# and VB.NET

    To scroll to the end of a TreeView in C# and VB.NET you can use the following snippet.

    Sample VB.NET / C#

    treeView1.Nodes[treeView1.Nodes.Count - 1].EnsureVisible();