Introduction
.NET has been around for quite some time. Do you even remember a time without .NET at all? Having so many years’ experience with .NET and its features, as well as Visual Studio, you would think that you know everything there is to know about .NET. Well, that is simply impossible. No one can know every little thing. So, the aim of this article is to introduce you to four cool features in .NET that you may not have known about.
Let’s start!
1. ObsoleteAttribute
ObsoleteAttribute marks program elements that are no longer in use. You could use this to produce an error when an Obsolete method has been called. A small example follows:
C#
public class ObsoleteExample { [ObsoleteAttribute("Method obsolete. Use NewMethod instead.", true)] public static string OldMethod() { return "Old Method."; } public static string NewMethod() { return "New Method."; } public static void Main() { Console.WriteLine(CallOldMethod()); } }
VB.NET
Public Class ObsoleteExample <ObsoleteAttribute("Method obsolete. Use _ NewMethod instead.", True)> Public Shared Function OldMethod() As String Return "Old Method." End Function Public Shared Function NewMethod() As String Return "New Method." End Function Public Shared Sub Main() Console.WriteLine(CallOldMethod()) End Sub End Class
The preceding code will produce an error and inform the developer that the Method or Property he or she is attempting to use is obsolete and has been replaced with a different Method or Property.
2. DebuggerDisplay Attribute
The DebuggerDisplay attribute controls how an object, property, or field will be displayed in the debugger variable windows, such as the Watch window. The following example will display:
Age = 40
C#
[DebuggerDisplay("Age = {Age}")] class Student { public int Age = 40; }
VB.NET
<DebuggerDisplay("Age = {Age}")> Class Student Public Age As Integer = 40 End Class
3. Environment.FailFast Method
You can use Environment.FailFast instead of the Exit method to terminate your application immediately if the state of your application is damaged beyond repair. The Environment.FailFast method terminates a process without running any active try/finally blocks and/or finalizers. Here is a brief example:
C#
public static void Main() { string strFailureReason = "Critical Error Occurred, Exiting."; try { Environment.FailFast(strFailureReason); } finally { Console.WriteLine("finally will not be executed."); } }
VB.NET
Public Shared Sub Main() Dim strFailureReason As String = "Critical Error Occurred, _ Exiting." Try Environment.FailFast(strFailureReason) Finally Console.WriteLine("finally will not be executed.") End Try End Sub
4. volatile Keyword
volatile indicates that a field might be modified by multiple concurrent threads. Volatile Fields are not subject to compiler optimizations that assume access by a single thread, ensuring that the most up-to-date value is always present in the field. An example follows:
C#
class Program { static string Output; static volatile bool Completed; static void SetVolatile() { Output = "Testing Volatile"; Completed = true; } static void Main() { new Thread(new ThreadStart(SetVolatile)).Start(); Thread.Sleep(200); if (Completed) { Console.WriteLine(Output); } } }
There is no equivalent to C#’s volatile keyword in VB.NET. The C# keyword volatile ensures that the JIT compiler handles things differently when generating the IL (Intermediate Language), but the VB.NET compiler does not have this option. You could, however, make use of Threading.Thread.MemoryBarrier in your VB.NET code, as shown next:
VB.NET
Function VolatileRead(Of T)(ByRef Address As T) As T VolatileRead = Address Threading.Thread.MemoryBarrier() End Function Sub VolatileWrite(Of T)(ByRef Address As T, ByVal Value As T) Threading.Thread.MemoryBarrier() Address = Value End Sub
Conclusion
I have mentioned four features of .NET that I think are quite cool. Obviously, the list is longer, and as I find more, I will update this article. You may also send features that you think are cool or underused. Please feel free to add comments with your suggested features.