blog

Home / DeveloperSection / Blogs / Struct in C#.Net

Struct in C#.Net

Anonymous User6152 30-Dec-2010

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:

http://www.csharp-station.com/tutorials/lesson12.aspx

http://msdn.microsoft.com/en-us/library/aa288471(v=VS.71).aspx


Updated 18-Sep-2014
I am a content writter !

Leave Comment

Comments

Liked By