C# Structures

    1 Votes

In C# the structures are the value type data type that contains variables, methods, properties, events, etc. It also simplifies the program and enhances the performance of code. It also improves the speed and memory usage. It helps us to make a single variable hold related data of various data types.

The struct keyword is used for creating structure. They are used to represent record. Like if we want to record the details of student then the attributes will be:
  • Name
  • Roll no
  • Marks

 

Let us have an example to understand it better:
using System;
struct Student
{
   public string name;
   public int roll_no;
   public int marks;
};  
public class Program
{
   public static void Main(string[] args)
   {
      Student s1;  
      Student s2;  
      s1.name = "Samdish";
      s1.roll_no = 60; 
      s1.marks = 95;
      s2.name = "Raj";
      s2.roll_no = 2;
      s2.marks = 92;
      Console.WriteLine ( "Student 1 name: {0}", s1.name);
      Console.WriteLine ("Student 1 roll no: {0}", s1.roll_no);
      Console.WriteLine ("Student 1 marks: {0}", s1.marks);
      Console.WriteLine ("Student 2 name: {0}", s2.name);
      Console.WriteLine ("Student 2 roll no: {0}", s2.roll_no);
      Console.WriteLine ("Student 2 marks: {0}", s2.marks);
      Console.ReadKey ();
   }
}
Output:
Student 1 name: Samdish
Student 1 roll no: 60
Student 1 marks: 95
Student 2 name: Raj
Student 2 roll no: 2
Student 2 marks: 92

 

Features:

  • It can have defined constructors but not destructors.
  • It can have methods, fields, indexers, properties, operator methods and events.
  • It cannot inherit other structure.
  • It can implement one or more interface.
  • It cannot be used as a base for other structure.

Classes v/s Structures

Some of the differences between classes and structures are:

StructuresClasses
They are value types. They are reference types.
It does not support inheritance. It supports inheritance.
It cannot have default constructor. It can have default constructor.
It cannot have explicit parameterless constructor. It can have parameterless constructor.
The member variable initialization is not possible. The member variable initialization is possible.
It does not have destructor. It can have destructor.
Object can be created without using new keyword. Object cannot be created without using new keyword.
Planning to do an MBA?
A quick all-in-one MBA entry manual for MBA Aspirants. Book covers
  • Possible MBA Tests & Exam Preparation
  • Tips to choose right MBA Program
  • Essay, Resume & Letter of Recommendation
  • MBA Interview Preparation
  • MBA Financial Planning
Price - 6.99$
 

Popular Videos

How to speak to people

How to speak so that people want to listen.

Got a tip or Question?
Let us know

Related Articles

C# Overview & Evolution of C# Language
Introduction of .NET Framework
C# Datatypes - Datatype Conversion, Boxing & Unboxing
C# Metadata, Metadata Tables and Benefits
C# - Common Language Intermediate (CIL)
C# - Class and Objects
C# Assembly - How to create and share assembly
C# Garbage Collection
C# Indexers
C# Exception Handling - User-Defied Exceptions
C# Inheritance - Single, Hierarchical, Multilevel & Multiple Inheritance
C# Polymorphism - Static and Dynamic polymorphism
C# Abstract Classes
C# Delegates and Events
C# Collections
C# XML Read and Write
C# Windows Presentation Foundation (WPF)
Language Integrated Query (LINQ)