C# Structs works almost like a Class !

//it is also working like as class
//its one time definable
using System;


namespace CStructs
{
    public struct Rectangle
    {
        public int height, width;
    }



    class TestStructs
    {
        static void Main(string[] args)
        {
            Rectangle r=new Rectangle();
            r.height = 4;
            r.width = 5;
            Console.WriteLine("Area of rectangle is:"+(r.height*r.width));

        }
    }
}
//constrcutor and method is also possible with structs 
//struct does not suppost inheritance but it can implement intrfaces
using System;

namespace StructConstructor
{
    public struct Rectangle
    {
        public int width, height;

        public Rectangle(int w,int h)
        {
            width = w;
            height = h;
        }

        public void AreaOfRectangle()
        {
            Console.WriteLine("Area of rectangle:"+(width*height));
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Rectangle r=new Rectangle(5,6);
            r.AreaOfRectangle();
            Console.ReadKey();
        }
    }
}

 

 

It would be a great help, if you support by sharing :)
Author: zakilive

Leave a Reply

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