VB icon

C# enum example

Email
Submitted on: 5/21/2019 2:50:49 PM
By: Dave Bowlin 
Level: Beginner
User Rating: Unrated
Compatibility: C#
Views: 438
author picture
 
     A very simple demonstration of how easy ENUMs can be. To test this code, create a console app, copy and paste it, then run it.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: C# enum example
// Description:A very simple demonstration of how easy ENUMs can be. To test this code, create a console app, copy and paste it, then run it.
// By: Dave Bowlin
//**************************************

/* Enum example
 * 
 * enums are used to declare a list of named integer constants
 * which you can reference using the name, or the integer value
 * 
 * as usual, when counting indexes in C#, the number starts at zero
 * but you can change the starting index number by assigning a value to it
 * 
 * example: public enum Colors { Green = 25, Red, Blue, Orange }
 */
 using System;
namespace enum_example
{
public enum Colors { Green, Red, Blue, Orange, Yellow, Pink, Brown, White, Black, Teal, Wheat, Transparent} // our enum, which holds named colors
class Program
{
static void Main(string[] args)
{
Random r = new Random();
int i = Enum.GetValues(typeof(Colors)).Length; // get and display how many indexes in our Days enum
Console.WriteLine("Number of colors: " + i);
Array values = Enum.GetValues(typeof(Colors)); // put the enum values into an array
for (int x = 0; x <= i; x++)
{
Colors color = (Colors)values.GetValue(r.Next(values.Length)); // select random day
Console.WriteLine(x + ": " + color + ", index: " + ((int)color)) ; // display the random color, and the index
}
Console.ReadKey();
}
}
}


Other 68 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.