Structure

It is a value data type that holds a group of different data types, which can be accessed by a struct variable. A struct can have parameterized constructor, static constructor, constants, fields, methods, properties, indexers,

It is a value data type that holds a group of different data types, which can be accessed by a struct variable. A struct can have parameterized constructor, static constructor, constants, fields, methods, properties, indexers, events and nested types.

By default a struct’s access specifier is internal and you can define a struct to be public, private and internal.

Struct and class may look similar but there are some differences like

  • Classes are reference data type while struct is a value data type.
  • A struct doesn’t support Inheritance.
  • You can’t have a default constructor for a struct while classes do.
  • We can’t define a Struct’s member as abstract, virtual or protected.
using System;

namespace Structures
{

    //<Access Specifier> struct <struct name> /*Access specifier by default its internal, you can declare private,internal,public*/
    //{
    //    /*variable list*/
    //    <Access Specifier> string <variable name;
    //    
    //    /*method list and so on*/
    //}

    struct Movie
    {
        private string Name;
        private string Director;
        private string Hero;
        private string Heroine;

        public void InputValues()
        {
            Console.WriteLine("Enter Movie Name");
            Name = Console.ReadLine();

            Console.WriteLine("Enter Movie Director");
            Director = Console.ReadLine();

            Console.WriteLine("Enter Lead Actor");
            Hero = Console.ReadLine();

            Console.WriteLine("Enter Lead Actress");
            Heroine = Console.ReadLine();
        }
        public void display()
        {
            Console.WriteLine("Moview Name: {0}", Name);
            Console.WriteLine("Movie Director: {0}",Director);
            Console.WriteLine("Lead Actor: {0}",Hero);
            Console.WriteLine("Lead Actress: {0}",Heroine);
        }
    }
    
    class Program
    { 
        static void Main(string[] args)
        {
            Console.WriteLine("Working With Structures");

            Movie movie1 = new Movie();
            Movie movie2 = new Movie();

            movie1.InputValues();
            movie2.InputValues();

            Console.WriteLine("We have 2 movies");
            movie1.display();
            movie2.display();

            Console.ReadKey();
        }
    }
}

Lets, look at the below struct containing properties, static constructors and parameterized constructors in action.

using System;

namespace Structures
{

    struct Movie
    {
        private string Name { get; set; }   //Properties
        private string Director { get; set; }
        private string Hero { get; set; }
        private string Heroine { get; set; }

        static Movie() //Static Constructor to let others know a struct is created
        {
            Console.WriteLine("Working With Structures");
        }

        public Movie (string a, string b, string c, string d) //Parameterized constructor assigning default values.
        {
            Name = a;
            Director = b;
            Hero = c;
            Heroine = d;
        }

        public void InputValues()
        {
            Console.WriteLine("Enter Movie Name");
            Name = Console.ReadLine();

            Console.WriteLine("Enter Movie Director");
            Director = Console.ReadLine();

            Console.WriteLine("Enter Lead Actor");
            Hero = Console.ReadLine();

            Console.WriteLine("Enter Lead Actress");
            Heroine = Console.ReadLine();
        }
        public void display()
        {
            Console.WriteLine("Moview Name: {0}", Name);
            Console.WriteLine("Movie Director: {0}",Director);
            Console.WriteLine("Lead Actor: {0}",Hero);
            Console.WriteLine("Lead Actress: {0}",Heroine);
        }
    }
    
    class Program
    { 
        static void Main(string[] args)
        {
            Movie movie = new Movie("Bahubali-2", "S. S. Rajamouli", "Prabhas", "Anushka Shetty");
            movie.display();

            Movie movie1 = new Movie();
            movie1.display();

            Movie movie2 = new Movie();

            movie1.InputValues();
            movie2.InputValues();

            Console.WriteLine("We have 2 movies");
            movie1.display();
            movie2.display();

            Console.ReadKey();
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *