By 22/07/2015 9 Comments

C# Structures

  • In C# the structures are similar to classes.
  • Structures are value types and inherit the System.ValueType implicitly.
  • Structures are very useful for small programs.
  • A structure can be declared by using keyword “struct”.
  • A structure can defines fields, methods, properties, indexers, events and operator methods as its members.
  • Structures do not support inheritance so that cannot inherit other structures and classes.
  • Like classes, one or more interfaces can be implemented in a structure separated by comma.
  • Structures can defines the constructors but an explicitly default (null parametrized) constructor cannot be defined in structures because the default constructors are automatically defined by structures which cannot be overridden or changed.
  • The fields (instance variables) of a structure are initialized to its default values through its default constructor.
  • Like classes an object of a structure can be created using “new” keyword but it is an optional and not required.
  • By use of “new” keyword the specified constructor will be called. If “new” keyword is not used the object of a structure is still created but not initialized than it must be initialized manually.
  • The members of a structure cannot be declared as protected, virtual or abstract.
  • If you assign one structure to other structure the copy of a structure object is created.

Example – 1, structure with parametrized constructor:

using System;
namespace csharpBasic
{
    // A structure declaration.
    struct MarkSheet
    {
        public double AspMarks;
        public double JsMarks;
        string StudentName;

        // Parametrized constructor.
        public MarkSheet(double aspMarks, double jsMarks, string studentName)
        {
            this.AspMarks = aspMarks;
            this.JsMarks = jsMarks;
            this.StudentName = studentName;
        }

        // Read-only double type property.
        public double TotalMarks
        {
            get
            {
                return 200;
            }
        }

        // Return type methods which returns different types of information.
        public string GetStudentName()
        {
            return this.StudentName;
        }

        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)
        {
            // An object of a structure is being created.
            MarkSheet markSheet = new MarkSheet(85, 60, "Abc");

            // 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.GetStudentName());
            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: 60
        Total marks: 200
        Obtain marks: 145
        Percentage: 72.5
    
       */
    }
}


Example – 2, structure with default constructor:

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)
        {
            // An object creation of a structure by calling default constructor.
            MarkSheet markSheet = new 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
    
       */
    }
}


Example – 3, structure without constructor:

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





Next Tutorial →


← Previous Tutorial


Posted in: C# Basics, C#.NET

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 .