Resharper Helps to Find Bug – Enum Type Not Equal to Null Forever


The Resharper is a commercial plugin for Visual Studio, that makes VS a better IDE. One useful feature of Resharper is to mark useless (unused) code in grey colour, so you know you can remove them safely.

c-sharp-enum-null Resharper Helps to Find Bug - Enum Type Not Equal to Null Forever bug fixes c # code review

Compare C# enum to null, resharper

The enum type in C# cannot be assigned to null, if you do, you will get a compilation error:

Error	1	Cannot convert null to 'ConsoleApplication3.Program.Days' because it is a non-nullable value type	E:\csharp\ConsoleApplication3\ConsoleApplication3\Program.cs	13	19	ConsoleApplication3

However, the syntax is still correct if you try to compare the enum variable to null. However, this always evaluates to false.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
using System;
 
namespace ConsoleApplication3
{
    class Program
    {
        enum Days { Sun, Mon, Tue, Wed, Thu, Fri, Sat };
 
        static void Main(string[] args)
        {
            Days day = Days.Fri;
            day = null;
            if (day == null) Console.WriteLine("day == null");
        }
    }
}
using System;

namespace ConsoleApplication3
{
    class Program
    {
        enum Days { Sun, Mon, Tue, Wed, Thu, Fri, Sat };

        static void Main(string[] args)
        {
            Days day = Days.Fri;
            day = null;
            if (day == null) Console.WriteLine("day == null");
        }
    }
}

Resharper helps to do the code review. It makes it easy.

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
249 words
Last Post: Batch Check Machines Up/Down in IP ranges using Powershell
Next Post: How to get Time using Batch Command - Substring

The Permanent URL is: Resharper Helps to Find Bug – Enum Type Not Equal to Null Forever

Leave a Reply