學習雜記1:c#,順序泛型棧,泛型委派,Lambda,拓展方法

來源:互聯網
上載者:User

標籤:linq   nbsp   his   []   代碼   length   stack   表達   過程   

    最近又重新回過頭來學習C#,才發現之前學習的知識有多麼的淺顯,有些東西還是挺難懂的,但是比較有意思,例如協變,逆變,不變就挺有意思,但是也挺繞,下面是學習過程中寫的一些代碼,有點亂,就當日記記敘一下。

  

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace Genericity泛型{    //泛型委派    public delegate R MyDelegate<T, R>(T t);    class Program    {        static void Main(string[] args)        {                        MyStack<int> stack = new MyStack<int>(5);            var stack1 = new MyStack<string>(8);            while(!stack.IsFull)            {                stack.Push(2);            }            while(!stack.IsEmpty)            {                stack.Pop();            }            MyDelegate<string, string> s;            s = stack.PrintString;            //Lambda運算式            s += (string a) => { return a; };            Console.WriteLine(s("safa"));            Console.WriteLine("這個棧的長度為:{0}",stack.GetLength<int>());            Console.ReadKey();        }          }    //順序棧,泛型    public class MyStack<T>    {        T[] StackArray;        public int Length;        int StackPointer;                             public string PrintString(string a)        {            return a;        }        public bool IsEmpty        {            get            {                return StackPointer == 0 ? true : false;            }        }        public bool IsFull        {            get            {                return StackPointer >= Length ? true : false;            }        }        public MyStack(int _length)        {            Length = _length;            StackArray = new T[Length];            StackPointer = 0;        }        public void Push(T _value)        {            if (IsFull)            {                Console.WriteLine("棧已經滿");            }            StackArray[StackPointer++] = _value;        }        public void Pop()        {            if (IsEmpty)            {                Console.WriteLine("棧已空");            }            Console.WriteLine(StackArray[--StackPointer]);        }        public void StackPrint()        {            foreach (var i in StackArray)            {                Console.WriteLine(i);            }        }    }    //拓展方法    public static class ExtendStack    {        public static int GetLength<T>(this MyStack<T> ms)        {            return ms.Length;        }    }}

 

學習雜記1:c#,順序泛型棧,泛型委派,Lambda,拓展方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.