Structs And Enums : struct « Class Interface « C# / C Sharp






Structs And Enums

 

using System;
using System.Collections.Generic;
using System.Text;

struct Date {
    public Date(int ccyy, Month mm, int dd) {
        this.year = ccyy - 1900;
        this.month = mm;
        this.day = dd - 1;
    }

    public override string ToString() {
        return this.month + " " + (this.day + 1) + " " + (this.year + 1900);
    }

    private int year;
    private Month month;
    private int day;
}


enum Month {
    January, February, March, April,
    May, June, July, August,
    September, October, November, December
}
class Program {
    static void Entrance() {
        Month first = Month.December;
        Console.WriteLine(first);
        first++;
        Console.WriteLine(first);

        Date defaultDate = new Date();
        Console.WriteLine(defaultDate);

        Date halloween = new Date(2008, Month.October, 31);
        Console.WriteLine(halloween);
    }

    static void Main() {
        try {
            Entrance();
        } catch (Exception ex) {
            Console.WriteLine(ex.Message);
        }
    }
}

 








Related examples in the same category

1.Define struct and use it
2.Demonstrate a structureDemonstrate a structure
3.Copy a structCopy a struct
4.Structures are good when grouping dataStructures are good when grouping data
5.demonstrates a custom constructor function for a structuredemonstrates a custom constructor function for a structure
6.Defining functions for structs
7.demonstrates using a structure to return a group of variables from a functiondemonstrates using a structure to return a group of variables from a function
8.Demonstates assignment operator on structures and classes.Demonstates assignment operator on structures and classes.
9.Issue an error message if you do not initialize all of the fields in a structure
10.Illustrates the use of a structIllustrates the use of a struct
11.C# always creates a structure instance as a value-type variable even using the new operatorC# always creates a structure instance as a value-type variable even using the new operator
12.Calling a Function with a Structure ParameterCalling a Function with a Structure Parameter
13.Structs (Value Types):A Point StructStructs (Value Types):A Point Struct
14.Structs (Value Types):Structs and ConstructorsStructs (Value Types):Structs and Constructors
15.Conversions Between Structs 1
16.Conversions Between Structs 2