articles

Home / DeveloperSection / Articles / structure in c#

structure in c#

structure in c#

Ravi Vishwakarma 492 02-Jul-2021

Structure

 A structure is a collection of variables of different datatypes in single unit. It is same as class both are user defined data types. It help you to make single variable hold the related datatype. struct keyword are used to make a structure. This is a value type so it is faster than object of class. Structure are used to hold the small data values. Structure are used to make a record.

a. Structure have contain methods, variables, operator methods, properties, and events.

  1. Structure can’t contain the default constructor (parameter less constructor)
  2. Structure can implements the interface but not extends the class
  3. And also can’t doesn’t the inherit the another structure
  4. Structure is declare using struct keyword.

Syntax of structure –

namespace namepase-name{

 struct struct-name {

  datatype variable-name1;

  datatype variable-name2

  // also present constructor in structure

  Structure-name(arguments 1, argument2, ….)

{

  // initialization the fields of structure

  }

  datatype function-name()

{

 //body of faunction

}

// properties declaration

}

}

Example of structure –

 structure Rectangle{

 public int xAxis;

 public int yAxis;

}

Creating objects

We know that structure are same as the class then how to declare an object in structure. Two type of creating object of structure with and without new keyword. So

 Syntax

  Structure-name object ;

  And

  Structure-name object =new Structure-name();

Note

 If we create object using new key word then automatically parameter less constructor run and initialize the value in variables, if we print the value of variable then doesn’t show error easily print but we can’t use new keyword then doesn’t run the parameter less constructor so variable not initialize . if we print the value then showing the compilation error .

 structure Rectangle{

  public int xAxis;

  public int yAxis;

}

// new keyword with no error

 Rectangle rect=new Rectangle();

 Console.WriteLine(rect.xAxis);

 Console.WriteLine(rect.yAxis);

 //Without new keyword

 Rectangle rect1;

 Console.WriteLine(rect1.xAxis); // generate a compile time error so we have must initialize the value

 rect1.xAxis=10;

 rect1.yAxis=20;

 Console.WriteLine(rect1.xAxis); // no error

Constructor structure

 It can’t the create the parameter less constructor, it always create parameterized constructor.

structure Rectangle{

  public int xAxis;

  public int yAxis;

  // showing error

Rectangle(){

}

  public Rectangle(int xp,int yp)

{

xAxis=xp;

yAxis=yp;

}

}

Rectangle rect =new Rectangle(10,50);

Console.WriteLine(rect.xAxis);

Console.WriteLine(rect.yAxis);

Methods in constructor

 structure Rectangle{

  public int xAxis;

  public int yAxis;

  public void setLocation (int xp,int yp)

{

xAxis=xp;

yAxis=yp;

}

  Public static Rectangle getOject()

{

 return new Rectangle();

}

}

Rectangle rect = Rectangle.getObject();

Console.WriteLine(rect.xAxis); // 0

Console.WriteLine(rect.yAxis); // 0

Rect.setLocation(15,85); //methods calling

Console.WriteLine(rect.xAxis); // 15

Console.WriteLine(rect.yAxis); // 85

 Suppose we want to store a student record in database. Then there are some attributes of student

• Student name

• Rollno

• Father name

• Date of birth

• Address

• Subject

• Mother name

Example of structure –

using System;

namespace structdemo

{

    struct Student{

        public String studentName;

        public Student(String name)

        {

            studentName=name;

        }

        public void setStudentName(String name)

        {

            studentName=name;

        }

        public String getStudentName()

        {

            return studentName;

        }

        public static Student getObject()

        {

            return new Student('Demo of struct');

        }

    }

  class HelloWorld

  {

    static void Main ()

    {

        Student student=new Student();

      Console.WriteLine (student.getStudentName());

      student.setStudentName('ravi');

      Console.WriteLine (student.getStudentName());

      Student std=new Student('demo');

      Console.WriteLine (std.getStudentName());

      Student student1=Student.getObject();

       Console.WriteLine (student1.getStudentName());

    }

  }

}

Output

ravi

demo

Demo of struct

Difference between Class and structure

  1. Class is a reference type and structure is value type
  2. Class allocate memory in heap area but structure memory allocation in stack area
  3. We are used class for represent the lager value of data and structure are used to smaller value of data
  4. All predefined datatype comes under the reference type of category eg. String and object, but all the predefined datatype comes under the value type category like int(Int32), float, bool(Boolean) are structure.
  5. In case of class “new “ keyword is mandatory for creating an object in the class but in case of structure “new “ keyword is optional.
  6. In case of class we can initialize the variable and declaration of class but in structure can’t initialize the variable of structure at declaration.
  7. Default constructor (parameter less constructor) is possible in structure but in class is possible.
  8.  Class can inherit by the other class but in structure is not possible sowing an error.
  9. Interface implementation is possible in structure and class


c# c# 
Updated 02-Jul-2021
Hi, my self Ravi Vishwakarma. I have completed my studies at SPICBB Varanasi. now I completed MCA with 76% form Veer Bahadur Singh Purvanchal University Jaunpur.

Leave Comment


Please enter subject.
Please enter comments message.

Comments

Liked By