Skip to content

1.2 Advance

1. Data Annotations

Declare Custom Attribute

[System.AttributeUsage(System.AttributeTargets.Class)]
public class Display : System.Attribute
{
private string _name;
public Display(string name)
{
_name = name;
}
public string GetName()
{
return _name;
}
}

Example of use

[Display("My Class Name")]
public class MyClass
{
// ...
}

Data annotations can be used on both properties and classes.

Example of reading attribute

public static string GetDisplayAttributeValue()
{
System.Attribute[] attrs =
System.Attribute.GetCustomAttributes(typeof(MyClass));
foreach (System.Attribute attr in attrs)
{
var displayAttribute as Display;
if (displayAttribute == null)
continue;
return displayAttribute.GetName();
}
// throw not found exception or just return string.Empty
}

Alternative method to read an attribute

// write a static function like this:
public static string GetDisplayName<TModel, TProperty>(this TModel model, Expression<Func<TModel, TProperty>> expression)
{
return ModelMetadata.FromLambdaExpression<TModel, TProperty>(expression, new ViewDataDictionary<TModel>(model)).DisplayName;
}
// use the function:
string name = GetDisplayName(Model, m => m.Prop);

2. using keyword in C#

In C#, the using statement is used to ensure that resources are properly disposed of when they are no longer needed. This is particularly useful for objects that consume resources like file handles, database connections, or network connections, which need to be explicitly released.

There are two primary ways to use using in C#:

2.1 Using with a Single Object

This is the most common usage, where a single object that implements the IDisposable interface is used within a using block. When the block is exited, either normally or through an exception, the object’s Dispose method is automatically called.

using (var resource = new SomeDisposableResource())
{
// Use the resource here.
} // resource.Dispose() is automatically called here.

2.2 Using with Multiple Objects (C# 8.0 and later)

Starting with C# 8.0, you can declare multiple disposable objects within a single using statement.

using (var resource1 = new SomeDisposableResource())
using (var resource2 = new AnotherDisposableResource())
{
// Use resource1 and resource2 here.
} // resource1.Dispose() and resource2.Dispose() are automatically called here.

2.3 Using as a Declaration (C# 8.0 and later)

In C# 8.0 and later, you can also use using as a declaration without a block. The resource will be disposed at the end of the containing scope, such as a method.

using var resource = new SomeDisposableResource();
// Use the resource here.
// resource.Dispose() is automatically called at the end of the scope.

Example with a File Stream

Here is an example using using with a FileStream to read from a file:

using (var stream = new FileStream("example.txt", FileMode.Open))
using (var reader = new StreamReader(stream))
{
string content = reader.ReadToEnd();
Console.WriteLine(content);
} // Both stream and reader are disposed here.

This ensures that both the FileStream and StreamReader are properly disposed of, even if an exception occurs while reading the file.