C# attributes are a powerful way to add declarative “metadata” to your C# code. This course will teach you what the benefits of attributes are, how to leverage prebuilt attributes, and how to create and use your own custom attributes.
C# attributes are a powerful way to add declarative “metadata” to your C# code that can be used by visual designers at design time, control code compilation at build time, customize the experience during debugging, and control program execution at run time. In this course, C# Attributes: Power and Flexibility for Your Code, you'll learn how to leverage the power of attributes in your C# applications. First, you'll learn about some of the problems attributes can help you solve. Next, you'll understand how you can benefit from some of the useful prebuilt attributes and how to provide values for attributes constructors and properties. Finally, you'll explore how to go one step further and define and use your own custom attributes to give your solutions extra power and flexibility. When you’re finished with this course, you’ll have an appreciation for the power and flexibility C# attributes provide and how you can use them to expand your C# applications.
Course Overview Hi everyone. My name is Jason Roberts. Welcome to my course, C# Attributes: Power and Flexibility for Your Code. I'm a Microsoft MVP, freelance developer, and author. In this course, you're going to learn what attributes are and how attributes can give you the power to add a new dimension to your C# code. Some of the major topics that we'll cover in this course include understanding the hidden power of attributes, how to use and configure many of the prebuilt attributes provided to you, and even how to define and use your own attributes in your C# applications. By the end of this course, you'll understand how C# attributes work and how to make use of them to create even more powerful C# solutions. Before beginning the course, it's recommended that you have at least a basic understanding of C#. I hope you'll join me on this journey with the C# Attributes: Power and Flexibility for Your Code course at Pluralsight.
Understanding the Hidden Power of Attributes Hi everyone. My name is Jason Roberts. Welcome to this course, C# Attributes: Power and Flexibility for Your Code. In this first module, we're going to start off by understanding the hidden power of attributes. So we're going to kick off this module by getting a brief overview of what attributes are just in case you haven't seen them in your C# journey so far. We'll learn about the many and varied uses for attributes in your solutions, and we'll learn that attributes can provide benefits at code time, design time, and at run time. In this module, we'll also learn the different code elements that attributes can be applied to, and we'll also learn what happens if we have attributes applied to a class and then we inherit from that class. Finally, we'll head over to Visual Studio, and we'll set up the demo solution for the rest of this course. This course is organized into a number of modules, and we're currently in this module, Understanding the Hidden Power of Attributes. In the following module, we're going to learn how to leverage the power of prebuilt attributes supplied to us. And in doing so, we'll also learn how to configure attribute constructors and properties. Now we understand how to consume attributes in our code and configure those attributes, in the next module, we'll learn how we can actually create our own custom attributes and make use of them in our solutions. So let's kick off this module by learning what attributes are.
Leveraging the Power of Prebuilt Attributes in Your Code Hi, welcome back. In the previous module, we learned what attributes are and some of the potential use cases for them. In this module, we're going to learn how to use some of the prebuilt attributes that are available to us. In doing so, we'll get an appreciation for the many and varied uses for attributes. And along the way, we'll also learn how to configure attribute properties and supply values for both required and optional attribute constructor parameters. So we're going to kick off this module by gaining and understanding of attribute constructors and properties and how we go about setting them. We'll then see some of the prebuilt attributes in use, and we'll start off by learning how we can control the debugging experience and customize the way objects appear in the debugging window. We'll learn that we can mark code as deprecated to give developers a chance to modify their code, for example, before we remove parts of an API. We'll learn how we can use attributes to conditionally compile sections of code so, for example, only calling a method if the compilation mode is set to debug. In addition to applying attributes at the class level or the method level, we'll also learn that we can apply them to the assembly itself. And we'll then learn how we can use an attribute at the assembly level to expose internal code to other assemblies. So, for example, if you've inherited some legacy code, you may want to start writing tests against this code before you change it. One little known fact of attributes in C# is that we can also apply them to return values, so we'll see this in action. And then we'll also learn that we can specify data validation using attributes and also indicate that a class can be serialized and also that certain members of that class should not be included in serialization. So let's kick off this module by getting an understanding of attribute constructors and properties.
Gaining Flexibility and Expanding Your Solutions with Custom Attributes Hi, welcome back. In the previous module, we gained an appreciation for many of the different ways attributes can be used. We were, however, using other people's attributes in our code. In this module, we're going to learn how we can take control of attributes and actually create our own. So we're going to kick off this module by learning how to define where our custom attribute can be applied. For example, we can say that our custom attribute can only be applied to properties or fields. We'll also learn that when we're defining custom attributes, we can specify whether or not we want the attribute to be able to be applied multiple times to a single code element. And we'll also see how we can override the default and specify that the custom attribute, when applied, shouldn't be inherited by child classes. Next, we'll head over to Visual Studio, and we'll look at a series of demos. In the first demo, we're going to actually create a new custom display attribute for our ContactWriter solution. Once we've created this custom attribute, we'll modify our display code to actually make use of it. And in the process, we'll learn how we can use reflection to discover attributes that have been applied to objects. Once we've modified the display code, we'll actually go and apply it to our contact class that we created earlier in the course. The Display attribute that we're going to create is only going to be valid for properties and fields, so we can't add it at the class level. Instead, we'll go and create another custom attribute that we can actually apply at the class level. And we'll also go and create a third custom attribute, this time one that can be applied multiple times to a single code element. So let's kick of this module by learning how we can define where our custom attribute can be applied.