C# 7

来源:https://www.cnblogs.com/Fred1987/archive/2020/01/28/12238973.html
-Advertisement-
Play Games

static void LocalMethod() { Cube(100); void Cube(int x) => Console.WriteLine($"The cube of {x} is {x * x * x}"); } static void GoToDemo() { int i = 1; ...


 static void LocalMethod()
        {
            Cube(100);
            void Cube(int x) => Console.WriteLine($"The cube of {x} is {x * x * x}");
        }

        
        static void GoToDemo()
        {
            int i = 1;
            startLoop:
            if(i<=5)
            {
                Console.WriteLine(i);
                i++;
                goto startLoop;
            }
        }

deconstructor

class Rectangle
    {
        public readonly float Width, Height;

        public Rectangle(float width,float height)
        {
            Width = width;
            Height = height;
        }

        public void Deconstruct(out float width,out float height)
        {
            width = Width;
            height = Height;
        }
    }

 static void Main(string[] args)
        {
            var rect = new Rectangle(3, 4);
            (float width, float height) = rect;
            Console.WriteLine($"width:{width},height:{height}");
            Console.ReadLine();
        }
 static void Main(string[] args)
        {
            var rect = new Rectangle(3, 4);
            var (width, height) = rect;
            Console.WriteLine($"width:{width},height:{height}");
            Console.ReadLine();
        }
static void Main(string[] args)
        {
            var rect = new Rectangle(3, 4);
            (var width,var height) = rect;
            Console.WriteLine($"width:{width},height:{height}");
            Console.ReadLine();
        }
class Product
    {
        decimal currentPrice, sharesOwned;
        public decimal Worth
        {
            get
            {
                return currentPrice * sharesOwned;
            }
        }

        public decimal Worth2 => currentPrice * sharesOwned;

        public decimal Worth3
        {
            get => currentPrice * sharesOwned;
            set => sharesOwned = value / currentPrice;
        }

public decimal CurrentPrice { get; set; } = 123;


public int Maximum { get; } = 999;


    }


private decimal x;
        public decimal X
        {
            get
            {
                return x;
            }

            private set
            {
                x = Math.Round(value, 2);
            }
        }

Indexer

 class Sentence
    {
        string[] words = "The quick brown fox".Split();

        public string this[int wordNum]
        {
            get
            {
                return words[wordNum];
            }
            set
            {
                words[wordNum] = value;
            }
        }
    }

 static void Main(string[] args)
        {
            Sentence se = new Sentence();
            Console.WriteLine(se[3]);
            se[3] = "kangaroo";
            Console.WriteLine(se[3]);
            Console.ReadLine();
        }
static class StaticClass
    {
        static StaticClass()
        {
            Console.WriteLine("This is the static constructor of the static class!");
        }

        public static DateTime DT = DateTime.Now;
    }

Partial class,partial method.

 partial class PaymentForm
    {
       partial void ValidatePayment(decimal amount);
       public void InvokePartialMethod(decimal amount)
        {
            ValidatePayment(amount);
        }
    }

    partial class PaymentForm
    {
       partial void ValidatePayment(decimal amount)
        {
           if(amount>100)
            {
                Console.WriteLine("Luxury");
            }
            else
            {
                Console.WriteLine("Ok");
            }
        }
    }

static void Main(string[] args)
        {
            PaymentForm pf = new PaymentForm();
            pf.InvokePartialMethod(10);
            pf.InvokePartialMethod(101);
            Console.ReadLine();
        }

 

 internal class Countdown : IEnumerator
    {
        int count = 11;
        public object Current => count;

        public bool MoveNext() => count-- > 0;

        public void Reset()
        {
            throw new NotImplementedException();
        }
    }

static void Main(string[] args)
        {
            IEnumerator e = new Countdown();
            while(e.MoveNext())
            {
                Console.WriteLine(e.Current);
            }
            Console.ReadLine();
        }
 [Flags]
    public enum BorderSides
    {
        None=0,Left=1,Right=2,Top=4,Bottom=8
    }

static void Main(string[] args)
        {
            BorderSides leftRight = BorderSides.Left | BorderSides.Right;
            if((leftRight&BorderSides.Left)!=0)
            {
                Console.WriteLine("Includes Left!");
            }

            string formatted = leftRight.ToString();
            BorderSides s = BorderSides.Left;
            s |= BorderSides.Right;
            Console.WriteLine(s == leftRight);
            Console.ReadLine();
        }
[Flags]
    public enum BSS
    {
        None=0,Left=1,Right=2,Top=4,Bottom=8,
        LeftRight=Left|Right,
        TopBottom=Top|Bottom,
        All=LeftRight|TopBottom
    }

Nested types

public class TopLevel
    {
        public class Nested { }
        public enum Color { Red,Blue,Tan}
    }

static void Main(string[] args)
        {
            TopLevel.Color color = TopLevel.Color.Red;
            Console.WriteLine(color);
            Console.ReadLine();
        }

Generics express reusability with a "Template" that contains "placeholder" types.Generics when compared to inheritance,can increase type safety and reduce casting and boxing.

 public class Stack<T>
    {
        int pos;
        T[] data = new T[100];
        public void Push(T obj) => data[pos++] = obj;
        public T Pop() => data[--pos];
    }

static void Main(string[] args)
{
var stack = new Stack<int>();
stack.Push(5);
stack.Push(10);
WriteLine(stack.Pop());
WriteLine(stack.Pop());


Console.ReadLine();
}

 
static void Main(string[] args)
        {
            Func<int,int,int> fc = AddXY;
            int result = AddXY(10, 20);
            Console.WriteLine(result);
            Console.ReadLine();
        }

        private static int AddXY(int v1, int v2)
        {
            return v1 + v2;
        }

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • wxml內容: <view class="container"> <navigator class="search"> <icon type="search" size="13"></icon>搜索 </navigator> <view class="banner_box"> <swiper cla ...
  • 先上一個場景:假如你突然想做飯,但是沒有廚具,也沒有食材。網上購買廚具比較方便,食材去超市買更放心。 實現分析:在快遞員送廚具的期間,我們肯定不會閑著,可以去超市買食材。所以,在主線程裡面另起一個子線程去網購廚具。 但是,子線程執行的結果是要返回廚具的,而run方法是沒有返回值的。所以,這才是難點, ...
  • 創建線程的2種方式,一種是直接繼承Thread,另外一種就是實現Runnable介面。 這2種方式都有一個缺陷就是:在執行完任務之後無法獲取執行結果。 如果需要獲取執行結果,就必須通過共用變數或者使用線程通信的方式來達到效果,這樣使用起來就比較麻煩。 而自從Java 1.5開始,就提供了Callab ...
  • 一.利用多線程 直接new線程 使用線程池 二.採用Spring 的非同步方法去執行(無返回值) 在啟動類或者配置類加上 @EnableAsync 註解. 先把longTimeMethod 封裝到Spring的非同步方法中,這個方法一定要寫在Spring管理的類中,註意註解@Async @Async註解 ...
  • 問題:在多線程環境下,如何防止自己的變數被其它線程篡改 __ 答案:ThreadLocal. __ __ThreadLocal 不是用來解決共用對象的多線程訪問的競爭問題的,因為ThreadLocal.set() 到線程中的對象是該線程自己使用的對象,其他線程是不需要訪問的,也訪問不到的。當線程終止 ...
  • 眾所周知,微服務架構是由一眾微服務組成,項目中調用其他微服務介面更是常見的操作。為了便於調用外部介面,我們的常用思路一般都是封裝一個外部介面的客戶端,使用時候直接調用相應的方法。webservice或WCF的做法就是引用服務,自動生成客戶端。在webapi2.0里,我們都會手動封裝一個靜態類。那麼在 ...
  • 反射這個詞聽起來就很牛逼是吧? 嗯的確,反射是比較高級的特性,只有語言基礎很扎實的Dev們才應該使用它。 搞點反射,可以提高程式的靈活性、可擴展性、耦合度。 反射這東西,是為了動態地運行時載入,相比於靜態代碼。編譯的時候就是板上釘釘了。 就是說,如果你的程式需要在運行時搞一些晚綁定,動態載入或檢查對 ...
  • 滑鼠事件執行幾個關聯的任務。當滑鼠移到某個元素上時,可通過最基本的滑鼠事件進行響應。這些事件是MouseEnter(當滑鼠指針移到元素上時引發該事件)和MouseLeave(當滑鼠指針離開元素時引發該事件)。這兩個事件都是直接事件,這意味著他們不使用冒泡和隧道過程,而是源自一個元素並且只被該元素引發 ...
一周排行
    -Advertisement-
    Play Games
  • Dapr Outbox 是1.12中的功能。 本文只介紹Dapr Outbox 執行流程,Dapr Outbox基本用法請閱讀官方文檔 。本文中appID=order-processor,topic=orders 本文前提知識:熟悉Dapr狀態管理、Dapr發佈訂閱和Outbox 模式。 Outbo ...
  • 引言 在前幾章我們深度講解了單元測試和集成測試的基礎知識,這一章我們來講解一下代碼覆蓋率,代碼覆蓋率是單元測試運行的度量值,覆蓋率通常以百分比表示,用於衡量代碼被測試覆蓋的程度,幫助開發人員評估測試用例的質量和代碼的健壯性。常見的覆蓋率包括語句覆蓋率(Line Coverage)、分支覆蓋率(Bra ...
  • 前言 本文介紹瞭如何使用S7.NET庫實現對西門子PLC DB塊數據的讀寫,記錄了使用電腦模擬,模擬PLC,自至完成測試的詳細流程,並重點介紹了在這個過程中的易錯點,供參考。 用到的軟體: 1.Windows環境下鏈路層網路訪問的行業標準工具(WinPcap_4_1_3.exe)下載鏈接:http ...
  • 從依賴倒置原則(Dependency Inversion Principle, DIP)到控制反轉(Inversion of Control, IoC)再到依賴註入(Dependency Injection, DI)的演進過程,我們可以理解為一種逐步抽象和解耦的設計思想。這種思想在C#等面向對象的編 ...
  • 關於Python中的私有屬性和私有方法 Python對於類的成員沒有嚴格的訪問控制限制,這與其他面相對對象語言有區別。關於私有屬性和私有方法,有如下要點: 1、通常我們約定,兩個下劃線開頭的屬性是私有的(private)。其他為公共的(public); 2、類內部可以訪問私有屬性(方法); 3、類外 ...
  • C++ 訪問說明符 訪問說明符是 C++ 中控制類成員(屬性和方法)可訪問性的關鍵字。它們用於封裝類數據並保護其免受意外修改或濫用。 三種訪問說明符: public:允許從類外部的任何地方訪問成員。 private:僅允許在類內部訪問成員。 protected:允許在類內部及其派生類中訪問成員。 示 ...
  • 寫這個隨筆說一下C++的static_cast和dynamic_cast用在子類與父類的指針轉換時的一些事宜。首先,【static_cast,dynamic_cast】【父類指針,子類指針】,兩兩一組,共有4種組合:用 static_cast 父類轉子類、用 static_cast 子類轉父類、使用 ...
  • /******************************************************************************************************** * * * 設計雙向鏈表的介面 * * * * Copyright (c) 2023-2 ...
  • 相信接觸過spring做開發的小伙伴們一定使用過@ComponentScan註解 @ComponentScan("com.wangm.lifecycle") public class AppConfig { } @ComponentScan指定basePackage,將包下的類按照一定規則註冊成Be ...
  • 操作系統 :CentOS 7.6_x64 opensips版本: 2.4.9 python版本:2.7.5 python作為腳本語言,使用起來很方便,查了下opensips的文檔,支持使用python腳本寫邏輯代碼。今天整理下CentOS7環境下opensips2.4.9的python模塊筆記及使用 ...