Monday, 10 February 2014

Struct in C#

                                                                                                                                Previous....

                                                                                                                                    Next...... 


In C#, a structure is a value type data type. so it is stored in stack and it use enum,delegate and there may be constructor or not. The struct keyword is used for creating a structure.
Defining a Structure
structure is a homogeneous datatype means there can be more then one datatype. 
For example, here is the way you would declare the Book structure:
struct student
{
   public string name;
   public int class ;
   public string subject;
   public int id;
};  
The following program shows the use of the structure:
using System;
     
struct student
{
   public string name;
   public string aclass;
   public string subject;
   public int id;
};  

public class testStructure
{
   public static void Main(string[] args)
   {

     student student1;       
     student student2;       

      /* student1 detail */
      student1.name = "munesh";
      student1.class = "VI"; 
      student1.subject = "DOT Net programming";
      student1.id = 16;

      /* student 2 detail */
      student2.name = "rahul";
      student2.class = "VII";
      student2.subject =  "C programming";
      student2.id = 20;

      /* print student1 info */
      Console.WriteLine( "student 1 name : {0}", student1.name);
      Console.WriteLine("student1 class : {0}", student1.class);
      Console.WriteLine("student 1 subject : {0}", student1.subject);
      Console.WriteLine("student 1 Id :{0}", student1.id);

      /* print Book2 info */
      Console.WriteLine("student2 name : {0}", student2.name);
      Console.WriteLine("student 2 class : {0}", student2.class);
      Console.WriteLine("student2 subject : {0}", student2.subject);
      Console.WriteLine("student 2 id : {0}", student2.id);       

      Console.Readline();

   }
}
When the above code is compiled and executed, it produces the following result:
student 1 name : munesh
student 1 class : VI
student 1 subject : Dot Net Programming
student 1 id : 16
student 2 name : Rahul
student 2 class : VII
student 2 subject : C programming
student 2 id : 20


Class vs Structure go to this link

                                                                                 Previous....
                                                                                                                                    Next...... 

No comments:

Post a Comment

C# program Selection Sorting

Selection sort is a straightforward sorting algorithm. This algorithm search for the smallest number in the elements array and then swap i...