SoFunction
Updated on 2025-03-01

Comparison of the difference between C# and C++ enumeration and use cases

The difference between C++ and C# enumeration

1. C++

  • Enumerate each element in a type, which can be used directly without having to be called through type.
  • No ++ operation
#include <iostream>
using namespace std;
enum week{Monday,Thuesday};
int main()
{
    week day;
    day = Monday;
    day = Thuesday;
    //day = 4; Error Reported Type conversion error occurred    //day++; Error, no ++ operation    cout << day << endl;//The output result is 1    return 0;
}

2. C#

  • Each element in an enumeration type must be called through the form of a type.element
  • Can operate ++
using System;
using ;
using ;
using ;

namespace myEnum_Structure
{
    enum Week
    {
        Monday,
        Thuesday,
        Wednesday,
        Thursday,
        Friday,
        Saturday,
        Sunday
    }
    class Program
    {
        static void Main(string[] args)
        {
            Week day;
            day = ;
            (day);//Output Sunday            day++;
            (day);//Output 7        }
    }
}

C# enumeration case

1. Ordinary call

        public enum NoticeType
        {
            Notice = 'A',
            LabRule = 'H',
            HotInformation = 'N',
            Column = 'C',
            All = '1',
            Null = '0'
        }
        private void button1_Click(object sender, EventArgs e)
        {
            // Create a new enumeration type            NoticeType noticeType1 = ;

            //Convert the enum type to string d="Column"            string d = ();

            //Get the cardinality of the enum type 'C'            char dd = (char)noticeType1;

            //Get the corresponding enumeration type through the cardinality            NoticeType noticeType2 = (NoticeType)("A");//Notice

            //Get enum type by name            NoticeType noticeType3 = (NoticeType)(typeof(NoticeType), "Notice");
        }

2. Obtain description information

 		[Description("Member level")]
        enum MemberLevel
        {
            [Description("Gold Member")]
            gold = 1,
            [Description("Silver Member")]
            silver = 2,
            [Description("Bronze Member")]
            copper = 3
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value">Enum value</param>        /// <param name="isTop">Does it be a description of the top title</param>        /// &lt;returns&gt;&lt;/returns&gt;
        public static string GetDescription(this Enum value, bool isTop = false)
        {

            Type enumType = ();
            DescriptionAttribute attr = null;
            if (isTop)
            {
                attr = (DescriptionAttribute)(enumType, typeof(DescriptionAttribute));
            }
            else
            {
                // Get the enumeration constant name.                string name = (enumType, value);
                if (name != null)
                {
                    // Get the enumeration field.                    FieldInfo fieldInfo = (name);
                    if (fieldInfo != null)
                    {
                        // Get the attributes described.                        attr = (fieldInfo, typeof(DescriptionAttribute), false) as DescriptionAttribute;
                    }
                }
            }

            if (attr != null &amp;&amp; !())
                return ;
            else
                return ;
        }

Call

     		MemberLevel gold = ;
            (());
            ();

This is all about this article about the difference comparison and use cases between C# and C++ enumerations. I hope it will be helpful to everyone's learning and I hope everyone will support me more.