[Obsolete] Attribute
Marks a program element as obsolete or deprecated, generating a warning or error when used.
[Obsolete("This method is deprecated, use NewMethod instead.")]
public void OldMethod() { }
[Serializable] Attribute
Indicates that an object can be serialized for storage or transmission.
[Serializable]
public class SerializableObject { }
[DataContract] and [DataMember] Attributes
Used in Windows Communication Foundation (WCF) for specifying serializable classes and members.
[DataContract]
public class Person
{
[DataMember]
public string FirstName { get; set; }
}
[DllImport] Attribute
Used for calling functions from unmanaged code.
class Program
{
[DllImport("user32.dll")]
public static extern int MessageBox(int hWnd, string text, string caption, int type);
}
[Conditional] Attribute
Specifies that a method should be called conditionally based on compilation symbols.
class Program
{
[Conditional("DEBUG")]
public static void DebugMethod()
{
// This method will only be included if DEBUG is defined.
}
}
[ThreadStatic] Attribute
Declares thread-local storage for a field, giving each thread its own copy.
public class ThreadLocalExample
{
[ThreadStatic]
public static int threadLocalValue;
}
[Serializable] Attribute
Marks a class as serializable for binary serialization.
[Serializable]
public class SerializableClass { }
[DefaultValue] Attribute
Specifies the default value for a property or field.
public class Example
{
[DefaultValue(42)]
public int MyProperty { get; set; }
}
[Description] Attribute
Provides a description for a property, event, or component.
public class Example
{
[Description("This is a description.")]
public int MyProperty { get; set; }
}
[DisplayName] Attribute
Specifies a display name for a property or event.
public class Example
{
[DisplayName("Display Name")]
public int MyProperty { get; set; }
}
[Browsable] Attribute
Indicates whether a property or event should be displayed in a designer.
public class Example
{
[Browsable(false)]
public int MyProperty { get; set; }
}
[NonSerialized] Attribute
Prevents a field from being serialized.
[Serializable]
public class SerializableObject
{
[NonSerialized]
public int NonSerializableField;
}
[XmlIgnore] Attribute
Excludes a property from XML serialization.
public class Example
{
[XmlIgnore]
public int MyProperty { get; set; }
}
[XmlAttribute] Attribute
Specifies that a property should be serialized as an XML attribute.
public class Person
{
[XmlAttribute]
public string FirstName { get; set; }
}
[XmlElement] Attribute
Specifies that a property should be serialized as an XML element.
public class Person
{
[XmlElement]
public string FirstName { get; set; }
}
[Serializable] Attribute
Marks a method as a web method that can be called from a web service.
[WebService]
public class MyWebService
{
[WebMethod]
public string HelloWorld()
{
return "Hello, world!";
}
}
[Authorize] Attribute
Restricts access to a controller or action method to authorized users.
[Authorize]
public class SecureController : Controller
{
public IActionResult Index()
{
return View();
}
}
[Route] Attribute
Specifies the route template for an action method in ASP.NET Core.
[Route("api/[controller]")]
public class MyController : Controller
{
[HttpGet("{id}")]
public IActionResult Get(int id)
{
// Handle GET request
}
}
[Required] Attribute
Indicates that a property is required for model validation.
public class Person
{
[Required]
public string FirstName { get; set; }
}
[MaxLength] Attribute
Specifies the maximum length for a string property.
public class Person
{
[MaxLength(50)]
public string FirstName { get; set; }
}
[MinLength] Attribute
Specifies the minimum length for a string property.
public class Person
{
[MinLength(2)]
public string FirstName { get; set; }
}
[RegularExpression] Attribute
Defines a regular expression pattern for string validation.
public class Person
{
[RegularExpression(@"^[A-Za-z]+$")]
public string FirstName { get; set; }
}
[Key] Attribute
Specifies a primary key for an entity in Entity Framework.
public class Person
{
[Key]
public int Id { get; set; }
}
[ForeignKey] Attribute
Indicates a foreign key relationship in Entity Framework.
public class Order
{
public int CustomerId { get; set; }
[ForeignKey("CustomerId")]
public Customer Customer { get; set; }
}
[Table] Attribute
Specifies the table name for an entity in Entity Framework.
[Table("ProductTable")]
public class Product
{
public int Id { get; set; }
}
[Column] Attribute
Maps a property to a specific database column in Entity Framework.
public class Product
{
[Column("ProductName")]
public string Name { get; set; }
}
[StringLength] Attribute
Specifies the maximum and minimum string length for validation.
public class Person
{
[StringLength(50, MinimumLength = 2)]
public string FirstName { get; set; }
}
[JsonProperty] Attribute
Maps a property to a specific JSON property name during serialization and deserialization.
public class Person
{
[JsonProperty("first_name")]
public string FirstName { get; set; }
}
[JsonIgnore] Attribute
Excludes a property from JSON serialization.
public class Person
{
[JsonIgnore]
public string SecretData { get; set; }
}
[AllowedValues] and [DeniedValues] Attributes
AllowedValues restricts a property to specific values, while DeniedValues blocks certain values
public class Product
{
[AllowedValues("Chocolate", "Candy", "Fruit")]
[DeniedValues("Vegetable", "Meat")]
public required string Title { get; set; }
}
[Base64String] Attributes
Ensures that the property contains a valid Base64-encoded string
public class Document
{
[Base64String]
public required string EncodedContent { get; set; }
}
[Length] Attributes
Restricts the length of the string between 5 and 50 characters
public class UserProfile
{
[Length(5, 50)]
public required string Username { get; set; }
}
[Range] Attributes
Ensures that the date is within the specified range (for DateOnly and TimeOnly types)
public class Event
{
[Range(typeof(DateOnly), "2024-01-01", "2025-12-31")]
public required DateOnly EventDate { get; set; }
[Range(typeof(TimeOnly), "08:00:00", "18:00:00")]
public required TimeOnly EventTime { get; set; }
}
[RequiredIf] Attributes
Makes the property required only if a condition is met (e.g., TrackingNumber is required if IsExpressShipping is true)
public class Order
{
[RequiredIf(nameof(IsExpressShipping), true)]
public string? TrackingNumber { get; set; }
public bool IsExpressShipping { get; set; }
}
[UnicodeCharacters] Attributes
Ensures that the string contains only Unicode characters
public class Message
{
[UnicodeCharacters]
public required string Content { get; set; }
}
[DisallowNull] Attributes
Prevents the property from being set to null
public class Settings
{
[DisallowNull]
public required string ConfigName { get; set; }
}
[DisallowDefault] Attributes
Prevents the property from being set to its default value (e.g., 0 for numeric types)
public class Transaction
{
[DisallowDefault]
public required decimal Amount { get; set; }
}
[DataType] Attributes
Specifies that the property should be treated as a specific data type (e.g., Currency and Duration)
public class Payment
{
[DataType(DataType.Currency)]
public required decimal TotalPrice { get; set; }
[DataType(DataType.Duration)]
public required TimeSpan ProcessingTime { get; set; }
}
[Display] Attributes
Enhances display attributes with localization support
public class Product
{
[Display(Name = "Product Name", Description = "The name of the product", ResourceType = typeof(Resources))]
public required string Title { get; set; }
}