কম্পিউটার

C# এ Type.GetEnumValues() পদ্ধতি


C#-এ Type.GetEnumValues() পদ্ধতিটি বর্তমান গণনার ধরণে ধ্রুবকের মানগুলির একটি অ্যারে প্রদান করে।

সিনট্যাক্স

নিচের সিনট্যাক্স −

public virtual Array GetEnumValues ();

উদাহরণ

আসুন এখন Type.GetEnumValues() পদ্ধতি প্রয়োগ করার জন্য একটি উদাহরণ দেখি -

using System;
public class Demo {
   enum Vehicle {Car, Bus, Bike, Airplane}
   public static void Main(){
      try {
         Type type = typeof(int);
         string[] str = type.GetEnumNames();
         Console.WriteLine("GetEnumNames() to return the constant name = " + str);
         Type type2 = type.GetEnumUnderlyingType();
         Console.Write("Enum Underlying type = "+type2);
         Array arrObj = type.GetEnumValues();
         Console.Write("Values = {0}"+arrObj);
         Console.WriteLine("\nListing constants ..");
         for (int i = 0; i < str.Length; i++)
            Console.Write("{0} ", str[i]);
      }
      catch (ArgumentException e){
         Console.WriteLine("Not an enum!");
         Console.Write("{0}", e.GetType(), e.Message);
      }
   }
}

আউটপুট

এটি নিম্নলিখিত আউটপুট −

তৈরি করবে
Not an enum!
System.ArgumentException

উদাহরণ

এখন Type.GetEnumValues() পদ্ধতি প্রয়োগ করার জন্য আরেকটি উদাহরণ দেখা যাক -

using System;
public class Demo {
   enum Vehicle {Car, Bus, Bike, Airplane}
   public static void Main(){
      try {
         Vehicle v = Vehicle.Bike;
         Type type = v.GetType();
         string[] str = type.GetEnumNames();
         Console.WriteLine("GetEnumName() to return the constant name = " + str);
         Type type2 = type.GetEnumUnderlyingType();
         Console.Write("Enum Underlying type = "+type2);
         Array arrObj = type.GetEnumValues();
         Console.Write("Values = "+arrObj);
         Console.WriteLine("\nListing constants ..");
         for (int i = 0; i < str.Length; i++)
            Console.Write("{0} ", str[i]);
      }
      catch (ArgumentException e){
         Console.WriteLine("Not an enum!");
         Console.Write("{0}", e.GetType(), e.Message);
      }
   }
}

আউটপুট

এটি নিম্নলিখিত আউটপুট −

তৈরি করবে
GetEnumName() to return the constant name = System.String[]
Enum Underlying type = System.Int32Values = Demo+Vehicle[]
Listing constants ..
Car Bus Bike Airplane

  1. C# এ Type.GetTypeFromHandle() পদ্ধতি

  2. C# এ Type.GetDefaultMembers() পদ্ধতি

  3. C# এ Type.GetArrayRank() পদ্ধতি

  4. C# এ Type.Equals() পদ্ধতি