Search code examples
c#attributescomvisible

ComVisible() vs ComVisibleAttribute()


Whats the difference between using [ComVisible()] and [ComVisibleAttribute()] ? They do exactly the same thing so whats the difference?

Thank you.


Solution

  • The common convention in .NET is to end all attribute classes with the suffix Attribute - so the actual class name is ComVisibleAttribute. This fits the best practices for extending a class and keeping its name as a suffix.

    However, this makes attribute names long and unwieldy, and clutters your code with a lot of boilerplate words. That's why the C# compiler know that when an attribute is specified, it searches both for the explicit name given, and if it isn't found, it will append Attribute to it.

    If you check the compiled source for your code, you'll see that both [ComVisible] and [ComVisibleAttribute] compile to exactly the same thing. It's just the compiler giving us some syntactic sugar to avoid seeing the word Attribute everywhere.