Hot File

Create Difference between struct and class in .NET?

View: 308    Dowload: 0   Comment: 0   Post by: hanhga  
Author: none   Category: Visual studio & .NET   Fields: Other

9 point/1 review File has been tested

Reference types can have null-reference, means some time have not any values

Introduction

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
      
       */
    }
}

Create Difference between struct and class in .NET?

Create Difference between struct and class in .NET? Posted on 24-08-2016  Reference types can have null-reference, means some time have not any values 9/10 308

Comment:

To comment you must be logged in members.

Files with category

 
Newsletter Email

File suggestion for you

File top downloads

logo codetitle
Codetitle.com - library source code to share, download the file to the community
Copyright © 2015. All rights reserved. codetitle.com Develope by Vinagon .Ltd