26 May

First-class support for generic Attributes in C# 11

Programming

min. read

Reading Time: 2 minutes

What are Attributes?

I think there is no better way to describe attributes than to quote MDSN

Attributes provide a powerful method of associating metadata, or declarative information, with code (assemblies, types, methods, properties, and so forth). After an attribute is associated with a program entity, the attribute can be queried at run time by using a technique called reflection. For more information, see Reflection (C#).

https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/attributes/

An example of usage would be like so:

[Serializable]
public class SampleClass
{
    // Objects of this type can be serialized.
}

They are useful for a sort of metadata like descriptions, translations, in the editor or in runtime editor add notations, etc.

The best use case I saw that uses attributes so far is Odin Inspector, they heavily use attributes to describe how to display fields and properties in the inspector for Unity Editor. This is brilliant how the previously hard task of creating custom editors turned into a child’s play.

code on an laptop

First-class support for Attributes

Previously if you wanted to use a custom type in the attribute, you would have to introduce a new field of type Type and use unsafe syntax to check if provided type in the attribute is the correct one.

This would look something like this in C# 10 and previous

[Filter(typeof(MyFilterType))]
public class SomeClass {...}

public class FilterAttribute : Attribute
{
    public Type FilterType { get; set; }

    public FilterAttribute(Type filterType)
    {
         if(!typeof(IFilterType).IsAssignableFrom(filterType))
             throw new Exception("Runtime exception");
         ...
    }
}

Now in C# 11 (which is still in preview at the time of writing this article) would look something like this.

[Filter<MyFilterType>]
public class SomeClass {...}

public class FilterAttribute<T> : Attribute where T: IFilterType
{
    // no more constructor, runtime checking and build time exception support
}

As you can see this is so much better than the previous approach and gives so many benefits!

laptop on a table displaying code with legs on it's side

Benefits of Attribute of type T

  • Code completion/Build time error checking
  • Performance, no more runtime check for a given type
  • Safe casting and better code completion

And the benefits do not end there, time spent on static typing for programmers would greatly benefit productivity!

Keep up the good work C# design team! I would love to see more features that!

Are you a Virtual Reality Expert with C# and want to join our team? Contact us!

https://prographers.com/contact


Let's talk

SEND THE EMAIL

I agree that my data in this form will be sent to [email protected] and will be read by human beings. We will answer you as soon as possible. If you sent this form by mistake or want to remove your data, you can let us know by sending an email to [email protected]. We will never send you any spam or share your data with third parties.

I agree that my data in this form will be sent to [email protected] and will be read by human beings. We will answer you as soon as possible. If you sent this form by mistake or want to remove your data, you can let us know by sending an email to [email protected]. We will never send you any spam or share your data with third parties.