Struct in C#.Net

A struct type is a value type thatis typically used to encapsulate small groups of related variables, suchas details of student.

Example:Create struct
    public structStudentDetails
        /// <summary>
        /// ID of student
        /// </summary>
        public string StudentID;
        /// <summary>
        /// Name of student
        /// </summary>
        public string Name;
        /// <summary>
        /// Father’s name of student
        /// </summary>
        public string FatherName;
        /// <summary>
        /// Address of student
        /// </summary>
        public string Address;


Example: Using struct

To use a struct,instantiate the struct and use it just like a class.

StudentDetails sd = new StudentDetails;
sd.studentID= '324';
sd.Name= 'XYZ';
sd.FatherName= 'ABC';
sd.Address= 'California, USA';

Difference between struct and class

Structs may seem similar to classes, but there areimportant differences that you should be aware of. First of all, classes arereference types and structs are value types. By using structs, you can createobjects that behave like the built-in types and enjoy their benefits as well.

Check these links, for further reading on struct:

Last updated:9/18/2014 1:23:53 PM
Anonymous User

Anonymous User

I am a content writter !


Leave Comment