structure in c#

Structure In C# -:  The struct keyword in C# is used to create value types. A struct is similar to a class in that it represents a structure with mainly field and method members. However, a struct is a value type, whereas a class is a reference type. Therefore, a struct variable directly stores the data of the struct, while a class variable only stores a reference to an object allocated in memory.

In other words we can say that the Structures are lightweight classes. Just like classes, structures have behaviours and attributes. As a value type, structures directly contain their value and they are stored on the stack. Because structures reside on the stack, keep them small. The implementation of structures in C# enforces the policy of using a structure as a lightweight class. The following list details the differences between structures and classes..

  • Structures are sealed and cannot be inherited.
  • Structures cannot inherit from classes or other structures.
  • A structure implicitly inherits from System.ValueType.
  • The default constructor of a structure cannot be replaced.
  • Custom constructors of a structure must initialize every field of that structure.
  • Structures cannot have destructors.
  • Fields cannot be initialized in the declaration of the structure. However, const members of a structure can be initialized.

Here is the syntax of a structure:

attributes accessibility struct identifier: interfacelist { body };

Structures support attributes. Actually, some attributes apply to structures explicitly—check the documentation of the attribute. Structures have the same accessibility options that a class does. Structures can implement interfaces. The structure body encompasses the member functions and fields of the structure.

The default constructor of a structure initializes each field to a default value. we cannot replace the default constructor of a structure. Unlike a class, adding constructors with parameters to a structure does not remove the default constructor. Invoke a custom constructor with the new operator. It allows us to call a constructor other than the default constructor.

The new operator will not place the structure on the managed heap. It is just another alternative to initializing the structure. Structures are commonly declared without the new operator. In that circumstance, the default constructor is called.

In the following code, Fraction is a structure that models a fraction. Fraction has two members—both doubles. The structure is small, which is ideal. The following code is an example:

using System;
namespace Donis.CSharpBook {

public struct Fraction {

public Fraction(double _divisor, double _dividend) {
divisor = _divisor;
dividend = _dividend;
                                                  }

public double quotient {
get {
return divisor / dividend;
}
}

private double divisor;
private double dividend;
}

public class Calculate {
public static void Main() {
Fraction number = new Fraction(4,5);
Console.WriteLine("{0}", number.quotient);
}
}
}

 

Struct Variable-: Structs share most of the same syntax as classes. For example, the following struct is named Point and consists of two public fields.

struct Point
{
       public int x, y;
}

Struct Constructors-: Structs can contain the same members that classes can, except that they cannot contain destructors or parameter-less constructors. The parameter-less constructor is automatically provided and may not be user-defined. However, a struct may declare constructors that have parameters. The compiler will then enforce that all struct fields are assigned in the constructors, so as to avoid problems associated with unassigned variables.

struct Point
      {
         public int x, y;
         public Point(int x, int y)
                       {
                        this.x = x;
                         this.y = y;
                       }
       }

Struct Field Initializers-: Fields within a struct cannot be given initial values, unless they are declared as const or static.

struct Point
{
     public int x = 1, y = 1; // compile-time error
     public static int myStatic = 5; // allowed
     public const int myConst = 10; // allowed
}

Struct Inheritance-: A struct cannot inherit from another struct or class, and it cannot be a base class. This also means that struct members cannot be declared as protected, private protected, or protected internal, and that struct methods cannot be marked as virtual. Structs implicitly inherit from System.ValueType, which in turn inherits from System.Object. Although structs do not support user-defined inheritance, they can implement interfaces in the same way as classes.

 

 

Leave a Reply

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