The User Defined data type are the type in which we can defined such data type according to our requirement. Some example of user defined data type are Enumeration and Structure Type.  Today we will discuss the user defined Data Types. In this article we will learn how to declare and define enumeration data type and structure data type. Also we will see that how we can access/ retrieve the values that are stored in these User defined Types. I hope that this will help you to learn something about Enumeration and Structure Type.




Enumeration Type:

Enumerators are useful when a variable can only have a specific set of values. By default, enumerator values start from 0.

  • Defining an Enumeration data type variable:- use the enum keyword followed by the enum
variable name and initial values.
enum Day { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday }
  • Using an Enumeration type Variable:- To access the single value from the group of values of the same enumeration type we use the following syntax.
Day dayofweek = Day.Monday;

Structure Type:

You can use structures to create objects that behave like built- in value types. Because structs are stored inline and are not heap allocated, there is less garbage collection pressure on the system than there is with classes.

  • Defining a Structure Type:- You can use a structure to group together several arbitrary types, as shown in the following example:
public struct Employee
{
string firstName;
int    age;
}
  • Using a Structure Type

To access elements inside the struct we need to first create an object of the structure and then we can access the elements like variables and method that we declare inside the structure.

 

Employee companyEmployee; // Declare variable

companyEmployee.firstName = "Joe" // Set value

companyEmployee.age = 23;
admin (155 Posts)


Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Set your Twitter account name in your settings to use the TwitterBar Section.