Skip to main content

C#: Program #48 (Generic Struct)

using System;
using System.Linq;


namespace Generics1
{
    class Program
    {
        static void Main(string[] args)
        {

            Rectangle<string> myobj = new Rectangle<string>("8", "6");
            Console.WriteLine(myobj.Area());
            Console.ReadKey();
        }
        public struct Rectangle<T>
        {
            private T length,width;
            public T Width { get { return width; } set { width = value; } }
            public T Length { get { return length; } set { length = value; } }

        public Rectangle(T w, T l)
        {
            width = w;
            length = l;
        }
        public string Area()
        {
            double no1=Convert.ToDouble(Width);
            double no2=Convert.ToDouble(Length);
            return Convert.ToString(no1*no2);
        }
        }
    }
}

Comments

Popular posts from this blog

C#: Program #51(Using Linq Extension Methods)

using System; using System.Collections.Generic; using System.Linq; namespace Linqmethods {     class Program     {         static void Main(string[] args)         {             List<int> tosses = new List<int>();             Random rndm = new Random();             int i=0;             while (i < 100)             {                 tosses.Add(rndm.Next(1,3));                 i++;             }             Console.WriteLine("HEAD:{0}",tosses.Where(x => x == 1).Count());             Console.WriteLine("TAIL:{0}", tosses.Where(x => x == 2).Count());         ...

C#: Program #52(Using Linq Extension Methods)

using System; using System.Collections.Generic; using System.Linq; namespace Linqmethods2 {     class Program     {         static void Main(string[] args)         {             List<int> series = new List<int>();             series.AddRange(Enumerable.Range(1, 20));             var squares = series.Select(a=>a*a).ToList();             foreach (int i in squares)             {                 Console.WriteLine(i);             }             Console.WriteLine("The sum of the two");             List<int> lone = new List<int>{1,5,6,3,2};             List<int> ltwo = new List...