Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
csharp enum
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
//Rextester.Program.Main is the entry point for your code. Don't change it. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5 using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public struct Colors { public enum Enum { Red = 0, Orange = 1, Yellow = 2, Green = 3, Blue = 4, Purple = 5, Brown = 6, Black = 7 } private Enum m_current_enum; public Colors(Colors.Enum in_enum) { m_current_enum = in_enum; } public Colors(int in_int) { m_current_enum = (Enum)in_int; } public static implicit operator Colors(int in_int) { return new Colors(in_int); } public static implicit operator Colors(Colors.Enum in_enum) { return new Colors(in_enum); } public static explicit operator int(Colors obj) { return (int)obj.m_current_enum; } public static bool operator == (Colors obj1, Colors obj2) { return obj1.m_current_enum == obj2.m_current_enum; } public static bool operator != (Colors obj1, Colors obj2) { return obj1.m_current_enum != obj2.m_current_enum; } public override bool Equals( object obj ){ if( obj is Colors ) { Colors o = (Colors)obj; return m_current_enum == o.m_current_enum; } if (obj is Colors.Enum ) { Colors.Enum e = (Colors.Enum)obj; return m_current_enum == e; } return false; } public override int GetHashCode() { return m_current_enum.GetHashCode(); } public override string ToString() { return m_current_enum.ToString(); } public static Enum FromString(string in_str) { switch (in_str) { case "Red": return Enum.Red; case "Orange": return Enum.Orange; case "Yellow": return Enum.Yellow; case "Green": return Enum.Green; case "Blue": return Enum.Blue; case "Purple": return Enum.Purple; case "Brown": return Enum.Brown; case "Black": return Enum.Black; } //Throw return Enum.Red; } public static Enum RedAlias = Enum.Red; public static Enum Red = Enum.Red; public static Enum Orange = Enum.Orange; public static Enum Yellow = Enum.Yellow; public static Enum Green = Enum.Green; public static Enum Blue = Enum.Blue; public static Enum Purple = Enum.Purple; public static Enum Brown = Enum.Brown; public static Enum Black = Enum.Black; } public class Program { public static void Main(string[] args) { Console.WriteLine("assignment, just like enum:"); Colors c = Colors.Orange; Console.WriteLine("convert enum to int, int to enum:"); Console.WriteLine((int)c); Console.WriteLine((Colors)1); Console.WriteLine(c==Colors.Orange); Console.WriteLine((Colors)1==Colors.Orange); Console.WriteLine(c.Equals((Colors)1)); Console.WriteLine(c==(Colors)1); Console.WriteLine((Colors)1==Colors.Orange); Console.WriteLine("ToString:"); Console.WriteLine(c.ToString()); Console.WriteLine(Colors.Red.ToString()); Console.WriteLine(Colors.Enum.Red.ToString()); Console.WriteLine("Alias:"); Console.WriteLine(Colors.RedAlias.ToString()); Console.WriteLine("FromString:"); Console.WriteLine(Colors.FromString("Red").ToString()); Console.WriteLine(Colors.FromString("Orange").ToString()); Console.WriteLine("List Compare:"); List<Colors> vec = new List<Colors>() { Colors.Red, Colors.Orange }; Console.WriteLine(vec==vec); //Colors z = (Colors)null; //Colors.Enum e = (Colors.Enum)null; } } }
Show compiler warnings
[
+
]
Show input
Compilation time: 0,12 s
edit mode
|
history
|
discussion