Chapter: 5 Structure

By Haitomns G

5.1    Introduction to structure, its features and its necessities

Structures (often referred to as structs) are similar to classes in C#. Although classes will be used to implement most objects, it is desirable to use structs where simple composite data types are required. Because they are value types stored on the stack, they have the following advantages compared to class objects stored on the heap:

  • They are created much more quickly than heap-allocated types.
    • They are instantly and automatically deallocated once they go out of scope.
    • It is easy to copy value type variables on the stack.

The performance of programs may be enhanced by judicious use of structs.

Features of Structure

  • Structures can have methods, fields, indexers, properties, operator methods, and events.
  • Unlike classes, structures cannot inherit other structures or classes.
  • Structures cannot be used as a base for other structures or classes.
  • A structure can implement one or more interfaces.
  • Structure members cannot be specified as abstract, virtual, or protected.

Necessities of Structure

Structure is necessary because it helps you to create class of different datatype and make as much object of it as required in the program. Structure is a user-defined datatype in C language which allows us to combine data of different types together. Structure helps to construct a complex data type which is more meaningful. It is somewhat similar to an Array, but an array holds data of similar type only, but structure can hold different datatypes.

5.2    Demonstration of Defining of structure and its usage

Structs are declared using the struct keyword. The simple form of a struct definition is as follows:

struct struct-name

{

data member1;

data member2;

.  .  .

.  .  .

                }

                Example:

                                struct Student{

                                                string name;

                                                int class;

                                                int roll;

                                }

                To create and item or object of the structure we need to use the following syntax:

struct-name item-name;

Example:

                Student s1;

Assigning Values to Members

Member variables can be accessed using the simple dot notation as follows:

                                item-name.variabl_name;

                Example:

                                s1.name = “Sam”;

                                s1.class = 9;

                                s1.roll = 22;

                Code:

                                using System;

struct Books {

   public string title;

   public string author;

   public string subject;

   public int book_id;

}; 

public class testStructure {

   public static void Main(string[] args) {

      Books Book1;   /* Declare Book1 of type Book */

      Books Book2;   /* Declare Book2 of type Book */

      /* book 1 specification */

      Book1.title = “C Programming”;

      Book1.author = “Nuha Ali”;

      Book1.subject = “C Programming Tutorial”;

      Book1.book_id = 6495407;

      /* book 2 specification */

      Book2.title = “Telecom Billing”;

      Book2.author = “Zara Ali”;

      Book2.subject =  “Telecom Billing Tutorial”;

      Book2.book_id = 6495700;

      /* print Book1 info */

      Console.WriteLine( “Book 1 title : {0}”, Book1.title);

      Console.WriteLine(“Book 1 author : {0}”, Book1.author);

      Console.WriteLine(“Book 1 subject : {0}”, Book1.subject);

      Console.WriteLine(“Book 1 book_id :{0}”, Book1.book_id);

      /* print Book2 info */

      Console.WriteLine(“Book 2 title : {0}”, Book2.title);

      Console.WriteLine(“Book 2 author : {0}”, Book2.author);

      Console.WriteLine(“Book 2 subject : {0}”, Book2.subject);

      Console.WriteLine(“Book 2 book_id : {0}”, Book2.book_id);      

      Console.ReadKey();

   }

}

Output:

Book 1 title : C Programming

Book 1 author : Nuha Ali

Book 1 subject : C Programming Tutorial

Book 1 book_id : 6495407

Book 2 title : Telecom Billing

Book 2 author : Zara Ali

Book 2 subject : Telecom Billing Tutorial

Book 2 book_id : 6495700

5.3    Compare and evaluate class vs structure and demonstrate it

image 7

                Code:

                                using System; 

public struct Rectangle 

    public int width, height; 

    public Rectangle(int w, int h) 

    { 

        width = w; 

        height = h; 

    } 

    public void areaOfRectangle() {  

     Console.WriteLine(“Area of Rectangle is: “+(width*height)); } 

    } 

public class TestStructs 

    public static void Main() 

    { 

        Rectangle r = new Rectangle(5, 6); 

        r.areaOfRectangle(); 

    } 

                Output:

Area of Rectangle is: 30

Haitomns G. is a desktop, android, and web developer based in Nepal. He has worked on building several websites, apps, and softwares for clients and independent projects. He is experienced in C, C++, C#, Java, Python, SQL, HTML, CSS, PHP, and JavaScript.

1 thought on “Chapter: 5 Structure”

Leave a Comment

Slide to prove you're not a bot/spammer *