Structures and Enumerations

By Bintu Chaudhary   Posted at  5:29:00 AM  

Home  »  C#  »  Structures and Enumerations

     A Structure is such a group of basic data type variables, which behaves as a unit. Moreover, it resembles another construct - class - a very important data type in object oriented programming.

Enumeration is yet another type of data structure that assigns integer values to string type data values. Both the data types are extensively used in C#.



     A Structure is a collection various variables with different variables like Int ,Float Char etc .The C# Language also provides us to create a data type which includes all the other data types of int , float and character The Main user defines data type is Structure Which Provides us the facility to make a data types which hold either the int value, float value of character values. Structure is the user defined data type which is used for creating a single Variable which makes us possible to declare a value either a int, float or character A user First Make a Structure of different data types with their respective variables A structures is like container which hold all the different variables with their different values and their different length.

Always Remember that the member of the Structure is always Accessed with the help of the (dot) . Operator or if we wants to access a variable of the structure then we must use the. (dot) Operator. The Member of the Structure are always Accessed through the Structure variables or For Accessing the Elements of the Structure First you have to create the instance of the Structure then with the help of the name of instance and dot Operator we can Access any variables from Structures for declaring the structure we can use this

struct stu
  char name[10],lastname[10];
  int age;
  float height;
Structure is also user defined data type but the Main difference between the Arrays and Structure is that All the Elements of An Array are of Same type but in Structure all the Elements of a Structure are of different data type.

A Structure May be Nested Means we can also define a Structure inside another Structure and Structure is a container of various Elements which have a different name and different data type.

What is Enumeration ?

     Enumerated data type is user defined data type and This is a Special data type which provides a user to opportunity to invent your own data type and define what values the variables of this data type can take Enumerated data type is used for crating a set of values of a date type and all the values of data type are start from 0 by default but a user and can change the value For Making this data type First we can declare the data type and specifies its possible values All the values are increment by 1 its previous value.

using System;
public class enumvariables
  public enum values
  public static void Main()
    Console.WriteLine("Value of D: " +values.d);

Value of D: 3

Enumeration are a collection of item (variables). It holds by default values if we not assign a value variable hold 0 value second 1 value third 2 and so on. If we assign the third variables 10 values. It show the fourth variable by default 11 value assign.

Difference Between the Structure and Class

  1. All the Variables in the Structure are value type so that they are stored on the Stack in Memory and in Class Variables are of reference type so that they are stored on the heap of the Memory.
  2. Classes Support Inheritance but a Structure can't be inherited.
  3. Classes have both default and Parameters Constructor but a Structure have not a default Constructor.
  4. When we Assign the Class Variable then the Original Values are Copied but in the Structure only the Values are Copied it doesn't Passes the Original Reference for that Variables.

Structs with Methods

     A C# Struct can also contain methods. The methods can be either static or non-static. But static methods can access only other static members and they can't invoke by using an object of the structure. They can invoke only by using the struct name.

Structs and Constructors

     A C# struct can declare constructor, but they must take parameters. A default constructor (constructor without any parameters) is always provided to initialize the struct fields to their default values. The parameterized constructors inside a struct can also be overload.

Structs with Arguments

Nested Structs

     We can declare nested structures i.e. structs inside other structs. Following code snippet illustrates this:

About the Author

Nulla sagittis convallis arcu. Sed sed nunc. Curabitur consequat. Quisque metus enim, venenatis fermentum, mollis in, porta et, nibh. Duis vulputate elit in elit. Mauris dictum libero id justo.
View all posts by: BT9

Back to top ↑
Connect with Me