By 11/02/2016 20 Comments

Difference between struct and class in .NET?

  • Classes are reference types.
  • Structs are value types.
  • All reference types live in heap memory.
  • All value types live in stack memory.
  • Classes have unlimited features.
  • Structs have limited features.
  • Classes are used for large programs.
  • Structs are used for small programs.
  • Value types always contain values.
  • Reference types can have null-reference, means some time have not any values.
  • The new keyword is required for for object creation of a class.
  • The new keyword is optional for struct.

Example: A class

using System;
namespace csharpBasic
{
    class Program
    {
        // Static main method void type declaration.
        static void Main(string[] args)
        {
            Console.ReadKey();
        } 
    }
 
    // Declare a Mark Sheet class.
    class MarkSheet
    {               
        /* Following is the initialization of string and double type class attributes/properties, 
           here these attributes are called fields. 
         */
        public string StudentName = "abc";
        public string StudentAddress = "xyz";
        public string Asp = "Asp.net";
        public string Javascript = "JavaScript";
 
        public double AspMarks = 80;
        public double JavascriptMarks = 60;
 
    }    // End class definition scope.
}

Example: A struct

using System;
namespace csharpBasic
{
    // A structure declaration.
    struct MarkSheet
    { 
        public double AspMarks;
        public double JsMarks;
        public string StudentName;
 
        // read only double type property
        public double TotalMarks
        {
            get
            {
                return 200;
            }
        }
 
        // Return type methods which returns different types of information.
        public double GetObtainMarks()
        {
            return this.AspMarks + this.JsMarks;
        }
 
        public double GetPercentage(double obtainMarks, double totalMarks)
        {
            return (obtainMarks / totalMarks) * 100.0;
        }
    }
 
    class Program
    {
        static void Main(string[] args)
        {
            // Reference of a structure type is declared.
            MarkSheet markSheet;
 
            // Initialize each field with different values.
            markSheet.StudentName = "abc";
            markSheet.AspMarks = 85;
            markSheet.JsMarks = 65;
 
            // Local variables declaration
            double obtainMarks, totalMarks;
 
            // Call a property and assign to a local variable.
            totalMarks = markSheet.TotalMarks;
 
            // call a method and assign to a local variable.
            obtainMarks = markSheet.GetObtainMarks();
 
            // Display different kind of mark sheet information.
            Console.WriteLine("Mark sheet information: ");
            Console.WriteLine("Student name: {0} ", markSheet.StudentName);
            Console.WriteLine("Asp.NET marks: {0} ", markSheet.AspMarks);
            Console.WriteLine("Javascript marks: {0}", markSheet.JsMarks);
            Console.WriteLine("Total marks: {0}", totalMarks);
            Console.WriteLine("Obtain marks: {0} ", obtainMarks);
            Console.WriteLine("Percentage: {0} ", markSheet.GetPercentage(obtainMarks, totalMarks));
 
            Console.ReadKey();
        }
        /*
        The Output will be:
         
        Mark sheet information:
        Student name: abc
        Asp.NET marks: 85
        Javascript marks: 65
        Total marks: 200
        Obtain marks: 150
        Percentage: 75
     
       */
    }
}






Posted in: C#.NET, Solutions

Leave your valuable Comment

Have a natural attraction for women cosmetics and replica watches uk clothes, no
more than two for men the most attractive one, is to make their own driving experience, happy and can serve as the facade of the car, another is to highlight the taste edify sentiment rolex replica watch. The replica rolex is undoubtedly the most fashionable accessories, wear a suit to attend the activities, but also get a decent match on the replica watches .