简体   繁体   中英

What is the difference between C# Attribute and C# Static Field?

According to Microsoft documents,

Attributes provide a powerful method of associating metadata, or declarative information, with code.

The static member is callable on a class even when no instance of the class has been created. The static member is always accessed by the class name, not the instance name.

But according to my understanding, both of the Attribute and Static Field are

  1. Can be accessed at runtime
  2. Bind with class, not object.

So what are the difference between them?

DEFINITION

A static data field or property is a variable encapsulated in the abstraction of a class definition.

Attribute is a decoration , a parameter , not a data itself, added to a class type or to a member field, property or method.

An attribute itself does nothing and represent nothing in terms of data or code: it is a conceptual artifact, added to the code, that can be used by the code, by some method, to change the behavior.

Attribute is a class that can contains data and methods to manage some behaviors of a class.

Attributes allows to segregate more between abstraction and encapsulation.

They allow to better refine design as well as to specialize behaviors at coding time while allowing generalization.

EXAMPLE

Consider this class:

public class LogFile
{
  static public List<LogFile> LogFiles { get; private set; }
}

Here it is a static list that contains all log files instantiated.

It is a data, a variable, something concrete and tangible, directly usable by internal and external code.

Here is an example of attribute usage:

[LogFileStorage(LogFilePath.User)]
public class LogFile
{
}

Here it is a code tag added to the class definition at the code level, that can't be changed, unless using reflexion, and that can be used by the logfile manager class to define a save path like in a temp folder or the user app folder or a runtime defined path (in a different and detached way from a constant or a default value).

So in this example, the log file save method will check the class attributes to know where to save it.

An attribute lets coders to parameterize class they define as well as child class and any class member.

A classical example is the Serializable attribute:

https://docs.microsoft.com/dotnet/api/system.serializableattribute

TUTORIALS

https://www.tutorialspoint.com/csharp/csharp_attributes.htm

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

https://docs.microsoft.com/dotnet/csharp/tutorials/attributes

https://www.tutorialspoint.com/csharp/csharp_encapsulation.htm

https://www.c-sharpcorner.com/UploadFile/mkagrahari/introduction-to-object-oriented-programming-concepts-in-C-Sharp/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM