using System; namespace CSharpColors { // declare a struct named Colour public struct Colour { public int red { get; set; } public int green { get; set; } public int blue { get; set; } // Constructor public Colour(int rVal, int gVal, int bVal) : this() { red = rVal; green = gVal; blue = bVal; } // Display the Struct as a String public override string ToString() { return (String.Format("{0}, {1}, {2}", red, green, blue)); } } // End struct public class Program { public void Run() { // Create an instance of the struct where red=100, green=50 and blue=250 Colour colour1 = new Colour(100, 50, 250); // Display the values in the struct //This will call automatically the "toString" method that resides in "Struct Colour" Console.WriteLine("Colour1 Initialized using the non-default constructor: "); Console.WriteLine("colour1 colour: {0}", colour1); Console.WriteLine(); // Invoke the default constructor // The colors are not set and will have the default value. Colour colour2 = new Colour(); Console.WriteLine("Colour2 Initialized using the default constructor: "); Console.WriteLine("colour2 colour: {0}", colour2); Console.WriteLine(); // Pass the struct to a method //This method will set the colors myFunction(colour1); // Redisplay the values in the struct Console.WriteLine("Colour1 after calling the method 'myFunction': "); Console.WriteLine("colour1 colour: {0}", colour1); Console.WriteLine(); } // Method takes a struct as a parameter public void myFunction(Colour col) { // Modify the values through the properties col.red = 200; col.green = 100; col.blue = 50; Console.WriteLine(); Console.WriteLine("colour values from inside the 'myFunction' method: {0}", col); Console.WriteLine(); } static void Main() { //Initialize the class Program Program t = new Program(); //Run It t.Run(); Console.ReadLine(); } } }