C# 基礎(更新中)

来源:https://www.cnblogs.com/FannyChung/archive/2020/05/31/csharpbasic.html
-Advertisement-
Play Games

Data StructureThere're two types of variables in C#, reference type and value type.Enum:enum Color{Red=0,Green=1}//equals to enum Color{Red,//start fr... ...


Data Structure

There're two types of variables in C#, reference type and value type.
Enum:

enum Color{
Red=0,
Green=1
}

//equals to 
enum Color{
Red,//start from 0 as default
Green
}

int to enum:(Color)val

Arrays and Collections

Array

declare array:

new int[10]
new int[10]{xx,xx.....};
new int[]{aa,aa};

Collection

List<string> theList=new List<string>();//can use value type here, not only reference type
theList.Count;//size of the list

class

when calling same name class with different namespace, it will use class of same namespace first, then System namespace.

fields and properties

  1. can't use var to declare fields like var count=0,must use int count=0
  2. const is static, so we can't use them together like public static const int count=0
  3. rules of naming fields and property: first letter with uppercase is public, first letter with lowercase is private.

Auto-implemented property: remove their private fields.
initiate with default: public MyClass val {get;set;} = new MyClass();

anonymous type

for the cases:

  • only use in one function and not need for other functions
  • only exists for only short time, and will be stored to other places

var val=new {name='Alex', age=12};

Extensions

class TheExtensionClass{
    public int toInt(this string value){
        return int.Parse(value);
    }
}

//so that the function can be called on string
str.toInt();

function

out, in and ref

if use out/in/ref for value type variable, then it will convert to reference type.
must initiate out variable in the function before use it.

default parameter and named parameter

Default parameter

  • must declared after non-default parameter
  • if we don't want to give value to the first default parameter but want to give the second default parameter, we can't do this without named parameter

Named parameter:
can call the function and break the sequence of parameters, especially for default parameter

special function:lambda

for the cases:

  • short function
var res=theList.Find(MyFunc);
boolean MyFunc(Student aStudent){
    return studentName="abc";
}

theList.Find(student=>{
    return studentName="abc";
});

theList.Find(student=> studentName="abc");

Event and asynchronous programming

Event

for the cases:
when executing monitor function, it will automatically execute the monitored function

  1. declare Action variable in class A:Action action
  2. binding the action variable to f2 (monitored function) of class B:action+=f2
  3. trigger the event: in f3(monitor function) call the action with action();
    ### asynchronous programming
    like event
    RequestSupport(CallBackFunc)

Exception and log

Exception

try{
}catch{
}
try{
}catch(FormatException){
}
try{
}catch(FormatException e){
    print e;
    print e.Message;
}

Log


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

-Advertisement-
Play Games
更多相關文章
  • 14.異常處理 只要是人編寫的代碼,或多或少都會存在一些Bug,當這些Bug被程式捕捉之後,需要怎麼處理,就需要我們告訴代碼應該處理,通常稱之為異常處理。 14.1 什麼是異常 Python使用異常對象來表示異常狀態,併在遇到錯誤進引發異常,異常對象被捕捉到未處理,程式則會終止並顯示相應的錯誤信息, ...
  • 冒泡排序是一種簡單的排序演算法。 1 #pragma once 2 #include <iostream> 3 #include <assert.h> 4 using std::cout; 5 using std::endl; 6 template <typename T> void Swap(T & ...
  • Java生鮮電商平臺-生鮮電商中配送訂單解決方案(小程式/APP) 說明:在日常的生鮮電商平臺中,生鮮配送是一個不可獲取的環節,根據實際的業務場景,生鮮配送有兩個維度需要考慮 1. 時間維度。 說明:時間維度是指客戶一般什麼時候要,就是什麼時候配送的問題,如果是前置倉模式,那麼就需要29分必送達,如 ...
  • 註釋是為了使別人能看懂你寫的程式,也為了使你在若幹年後還能看得懂你曾經寫的程式而設定的。 註釋是寫給程式員看的,不是寫給電腦看的。所以註釋的內容,C語言編譯器在編譯時會被自動忽略,不會執行註釋的代碼! 方法一:使用// #include "stdafx.h" //main函數 :程式的入口函數,就好 ...
  • 有很多剛學習軟體測試的小伙伴,都會在網路上找尋各種學習資料,去提升自己的專業技能水平。因此,我決定定期分享我整理收集的一些軟體測試的測試工具下載、面試寶典、視頻教學合集。都整理好了,有需要的可以關註我(獲取方式在文末) 軟體測試的學習,不止是基礎理論,還需要學習測試工具的用法,如介面工具Postma ...
  • 女程式員是這麼徵婚的: SELECT * FROM 男人們 WHERE 未婚=true and 同性戀=false and 有房=true and 有車=true and 條件 in (帥氣,紳士,大度,氣質,智慧,溫柔,體貼,會浪漫,活潑,可愛,最好還能帶孩子) and 年齡 between(24 ...
  • API 概述 API(Application Programming Interface),應用程式編程介面。 Java API是一本程式員的 字典 ,是JDK中提供給我們使用的類的說明文檔。 這些類將底層的代碼實現封裝了起來,我們不需要關心這些類是如何實現的,只需要學習這些類如何使用即可。 所以我 ...
  • 0. 前言 該項目使用Maven進行管理和構建,所以需要預先配置好Maven。嗯,在這個系列里就不做過多的介紹了。 1. 創建項目 先創建一個pom.xml 文件,添加以下內容: <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http: ...
一周排行
    -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模塊筆記及使用 ...