C#泛型學習

風靈使發表於2018-09-12

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GenericDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine($"10+20={add(10, 20)}");
            Console.WriteLine($"10.5+20.9={add(10.5, 20.9)}");
            Console.WriteLine($"10+20.9={add(10, 20.9)}");
            Console.WriteLine(add("hello", " world"));
            Console.WriteLine("-----------------");
            Console.WriteLine(Sum(100));
            Console.ReadLine();
        }
        //泛型方法簡單使用
        static T add<T>(T a, T b)
        {
            dynamic a1 = a;
            dynamic b1 = b;
            return a1 + b1;
        }
        //泛型方法實現一個數求和
        static T Sum<T>(T a) where T : struct
        {
            dynamic sum = default(T);
            for (dynamic i = default(T); i <= a; i++)
            {
                sum += i;
            }
            return sum;
        }
    }
}

如圖:

這裡寫圖片描述


Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GenericsDemo
{
    class Program
    {
        //static void Main(string[] args)
        //{
        //    MyStack<int> stack1 = new MyStack<int>(5);
        //    stack1.Push(1);
        //    stack1.Push(2);
        //    stack1.Push(3);
        //    stack1.Push(4);
        //    stack1.Push(5);
        //    stack1.Push(6);
        //    Console.WriteLine(stack1.Pop());
        //    Console.WriteLine(stack1.Pop());
        //    Console.WriteLine(stack1.Pop());
        //    Console.WriteLine(stack1.Pop());
        //    Console.WriteLine(stack1.Pop());
        //    Console.WriteLine(stack1.Pop());
        //    MyStack<string> stack2 = new MyStack<string>(5);
        //    stack2.Push("產品1");
        //    stack2.Push("產品2");
        //    stack2.Push("產品3");
        //    stack2.Push("產品4");
        //    stack2.Push("產品5");
        //    stack2.Push("產品6");
        //    Console.WriteLine(stack2.Pop());
        //    Console.WriteLine(stack2.Pop());
        //    Console.WriteLine(stack2.Pop());
        //    Console.WriteLine(stack2.Pop());
        //    Console.WriteLine(stack2.Pop());
        //    Console.WriteLine(stack2.Pop());
        //    Console.Read();
        //}

        static void Main(string[] args)
        {
            //例項化泛型物件
            MyGeneric<int, Course, Teacher> myGeneric = new MyGeneric<int, Course, Teacher>();
            //給物件屬性賦值
            myGeneric.ProductList = new List<Course>()
            {
                new Course(){CourseName=".NET",Period=10},
                new Course(){CourseName="SQL Server",Period=20},
                new Course(){CourseName="TypeScript",Period=30}
            };
            myGeneric.Publisher = new Teacher()
            {
                Age = 28,
                Name = "Lex"
            };
            Course course = myGeneric.Buy(1);
            Console.WriteLine($"你買的課程是{course.CourseName},時長為{course.Period}分鐘,釋出者是{myGeneric.Publisher.Name},年齡{myGeneric.Publisher.Age}歲");
            Console.Read();
        }
    }

    /// <summary>
    /// 新增約束型別的泛型類
    /// </summary>
    /// <typeparam name="T1"></typeparam>
    /// <typeparam name="T2"></typeparam>
    /// <typeparam name="T3"></typeparam>
    class MyGeneric<T1, T2, T3>
        where T1 : struct //
        where T2 : class
        where T3 : new()
    {
        public List<T2> ProductList { get; set; } = new List<T2>();
        public T3 Publisher { get; set; } = new T3();
        public T2 Buy(T1 item)
        {
            dynamic num = item;
            return ProductList[num];
        }
    }
    class Course
    {
        public string CourseName { get; set; }
        public int Period { get; set; }
    }
    class Teacher
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }
}

MyStack.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GenericsDemo
{
    /// <summary>
    /// 使用泛型類實現出入棧通用類
    /// </summary>
    public class MyStack<T>
    {
        T[] stack;
        int size;//棧資料容量
        int stackPoint;//棧位置指標

        public MyStack(int size)
        {
            this.size = size;
            stack = new T[size];
            stackPoint = -1;
        }
        /// <summary>
        /// 入棧
        /// </summary>
        /// <param name="item"></param>
        public void Push(T item)
        {
            stackPoint++;
            if (stackPoint >= size)
            {
                Console.WriteLine("棧空間已滿");
                return;
            }
            stack[stackPoint] = item;
        }
        /// <summary>
        /// 出棧
        /// </summary>
        /// <returns></returns>
        public T Pop()
        {
            stackPoint--;
            if (stackPoint < 0)
            {
                Console.WriteLine("棧空間已空");
                return default(T);
            }
            T data = stack[stackPoint];
            return data;
        }
    }
}

如圖:

這裡寫圖片描述

相關文章