Structures in c# – Free C# Tutorials

In this blog we will discuss about the Structures in c#.Structures in c# is an user-defined data type that is quite similar to classes in c# but classes are reference type stored in the heap while structure is a value type stored in the stack.

  • We have already seen that c# supports two kind of value types namely predefined types and user-defined types.
  • We have defined and used the predefined types such as int,float, char etc.
  • c# allows us to define our own complex values types known as user-defined value types.There are two types of user defined value types we can define in c# namely:
  1. structures
  2. enumerations

Thus structures and enumerations are  user-defined types.

  • We know that the basic value types are stored in the stack,likewise the user defined types are already stored in the stack memory.

Structures

  • Structures in c#( often referred to as structs)  are similar to classes in c#.
  • Although classes will be used to implement many objects, it is preferable to use structs where simple composite data types are required.

Since these value types are stored in stack they have the following advantage than the objects that are stored in the heap.

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

Defining a  struct in c#

  • The structures in c# provides a unique way of packing together the data in different ways.;
  • It is a convenient tool for handling a group of logically related data items
  • It creates a template that may be used to define its data properties
  • Once the structure has been created we can declare variables similar to the defined type in the struct.
  • Structures are declared using the struct keyword.Example of structure definition is given below:
Struct struct_name
{
   data member1;
   data member2;
}

Example

Struct Student
{
  public String Name;
  public int RollNumber;
  public ouble TotalMarks;
}

  • The keyword struct is used to define a structure student that can hold variables of different data types.
  • These variables are known as members,fields or elements .
  • Here the Student is known as the identifier which can be used to create variables of type Student.

Example:

Student s1;

  • s1 is a variable of type Student and has three member variables ( Name,RollNumber,TotalMarks) as defined.

Assigning Values to Members in c#

Member variables can be accessed using a dot-operator.For Example

s1.Name  =  "jacob";

s1.RollNumber   =  12;

s1.TotalMarks  =   1124;

  • We can also use the member variables in the expressions for the calculations.For Example:

FinalMarks  =  s1.TotalMarks + 11.0;


Copying structs in c#

  • We can copy values of one structs to another structs.Example:

Student s2;  // s2 is declared

s2 = s1;

  • this will copy all the values from s1 to s2.
  • We can also create variables using the new operator.For Example

Student s3  =  new Student();

  • Now using the s3 object in the case the memory is created in the heap memory since we have used the new keyword, using the s3 object we can assign the values like s3.Name  =  “Jerry”;

Note– the data members in the structure are by default by ‘private’ and therefore they cannot be accessed outside the structure definition.The use of the access modifier ‘public’ means that anbody can access the data.

Example:

Struct ABC
{
   int a;     // private by default
   public int b;   
   private int c    // explicitly declared as private   
}
ABC abc;  // using abc we can access the data members in structure using the dot operator
abc.a=10;  // Error because a is private
abc.b=20; // because b is declared as public.

code:

using System;
Struct product
{
   public String name;
   public int code;
   public double price;
}
class ProductTest
{
   public static void Main()
   {
         Product p;  // declare a product
         // assigning values to its data members
         p.name = "chair";
         p.code = 101;
         p.price  = 200;
         // diplay the data
         Console.WriteLine(" product name : "  + p.name);
         Console.WriteLine(" product code : "  + p.code);
         Console.WriteLine(" product price: "  + p.price);
   }
}

output

product name: fan
product code: 101
product price: 200

This is all about the structure in C# . Hope you like this tutorial.

Leave a Comment

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