Tuesday, 6 June 2017

Structure in C#.

In C# the struct keyword is used for creating a structure. Structures are used to represent a record a structure stores its data in its kind and of it's  related data.
 Example. Any college or school office will have following details for each student.--
name
course
address
subject. 
roll_no.

Example..

using System;
namespace Structures
{
    struct Student
    {
        public string name;
        public string course;
        public string address;
        public string subject;
        public int roll_no;
    }; 
    class Program
    {
        static void Main(string[] args)
        {
            Student s1;  
            
            /* Student  Details */
            s1.name = "Sandeep";
            s1.course = "MCA";
            s1.address="New Delhi";
            s1.subject = "C# Programming";
            s1.roll_no = 123456;
         

            /* print Student info */
            Console.WriteLine("Student name : {0}", s1.name);
            Console.WriteLine("Student course : {0}", s1.course);
            Console.WriteLine("Student address : {0}", s1.address);
            Console.WriteLine("Student Subject : {0}", s1.subject);
            Console.WriteLine("Student roll_no :{0}", s1.roll_no);

            
            Console.ReadKey();
        }
    }
}

Share this


0 Comments

Advertisement