C#是一個優雅的、類型安全的面嚮對象語言。使用C#,開發者能夠在.NET框架下構建安全而強大的應用程序。web
更多關於C#的介紹數據庫
// 單行註釋以 // 開始 /* 多行註釋是這樣的 */ /// <summary> /// XML文檔註釋 /// </summary>
聲明應用用到的命名空間數組
using System; using System.Collections.Generic; using System.Data.Entity; using System.Dynamic; using System.Linq; using System.Linq.Expressions; using System.Net; using System.Threading.Tasks; using System.IO;
定義做用域,將代碼組織成包安全
namespace Learning { // 每一個 .cs 文件至少須要包含一個和文件名相同的類 // 你能夠不這麼幹,可是這樣很差。 public class LearnCSharp {
若是你之前用過 Java 或 C++ 的話,能夠直接跳到後文「有趣的特性」數據結構
public static void Syntax() { // 使用 Console.WriteLine 打印信息 Console.WriteLine("Hello World"); Console.WriteLine( "Integer: " + 10 + " Double: " + 3.14 + " Boolean: " + true); // 使用 Console.Write 打印,不帶換行符號 Console.Write("Hello "); Console.Write("World");
使用 <type> <name>
定義變量併發
Sbyte - 有符號 8-bit 整數 (-128 <= sbyte <= 127)mvc
sbyte fooSbyte = 100;
Byte - 無符號 8-bit 整數(0 <= byte <= 255)框架
byte fooByte = 100;
Short - 16-bit 整數,有符號 - (-32,768 <= short <= 32,767),無符號 - (0 <= ushort <= 65,535)asp.net
short fooShort = 10000; ushort fooUshort = 10000;
Integer - 32-bit 整數ide
int fooInt = 1; // (-2,147,483,648 <= int <= 2,147,483,647) uint fooUint = 1; // (0 <= uint <= 4,294,967,295)
Long - 64-bit 整數
long fooLong = 100000L; // (-9,223,372,036,854,775,808 <= long <= 9,223,372,036,854,775,807) ulong fooUlong = 100000L; // (0 <= ulong <= 18,446,744,073,709,551,615)
數字默認爲 int 或 uint (取決於尺寸)
使用 L 標明變量值類型爲long 或 ulong
Double - 雙精度 64-bit IEEE 754 浮點數
double fooDouble = 123.4; // 精度: 15-16 位
Float - 單精度 32-bit IEEE 754 浮點數
float fooFloat = 234.5f; // 精度: 7 位
使用 f 標明變量值類型爲float
Decimal - 128-bits 數據類型,比其餘浮點類型精度更高,適合財務、金融
decimal fooDecimal = 150.3m;
布爾值 - true & false
bool fooBoolean = true; // 或 false
Char - 單個 16-bit Unicode 字符
char fooChar = 'A';
字符串 -- 和前面的基本類型不一樣,字符串不是值,而是引用。這意味着你能夠將字符串設爲null。
string fooString = "\"escape\" quotes and add \n (new lines) and \t (tabs)"; Console.WriteLine(fooString);
你能夠經過索引訪問字符串的每一個字符:
char charFromString = fooString[1]; // => 'e'
字符串不可修改: fooString[1] = 'X' 是行不通的。
根據當前的locale設定比較字符串,大小寫不敏感
string.Compare(fooString, "x", StringComparison.CurrentCultureIgnoreCase);
基於sprintf的字符串格式化
string fooFs = string.Format("Check Check, {0} {1}, {0} {1:0.0}", 1, 2);
日期和格式
DateTime fooDate = DateTime.Now; Console.WriteLine(fooDate.ToString("hh:mm, dd MMM yyyy"));
使用 @ 符號能夠建立跨行的字符串。使用 "" 來表示 "
string bazString = @"Here's some stuff on a new line! ""Wow!"", the masses cried";
使用const或read-only定義常量,常量在編譯期演算
const int HOURS_I_WORK_PER_WEEK = 9001;
聲明數組的格式以下:
// <datatype>[] <var name> = new <datatype>[<array size>]; int[] intArray = new int[10];
聲明並初始化數組的其餘方式:
int[] y = { 9000, 1000, 1337 };
訪問數組的元素
Console.WriteLine("intArray @ 0: " + intArray[0]);
數組能夠修改
intArray[1] = 1;
列表比數組更經常使用,由於列表更靈活。
聲明列表的格式以下:
// List<datatype> <var name> = new List<datatype>(); List<int> intList = new List<int>(); List<string> stringList = new List<string>(); List<int> z = new List<int> { 9000, 1000, 1337 }; // i
<>
用於泛型 - 參考下文
列表無默認值,訪問列表元素時必須首先添加元素
intList.Add(1); Console.WriteLine("intList @ 0: " + intList[0]);
其餘數據結構:
Console.WriteLine("\n->Operators"); int i1 = 1, i2 = 2; // 多重聲明的簡寫形式
算術直截了當
Console.WriteLine(i1 + i2 - i1 * 3 / 7); // => 3
取餘
Console.WriteLine("11%3 = " + (11 % 3)); // => 2
比較操做符
Console.WriteLine("3 == 2? " + (3 == 2)); // => false Console.WriteLine("3 != 2? " + (3 != 2)); // => true Console.WriteLine("3 > 2? " + (3 > 2)); // => true Console.WriteLine("3 < 2? " + (3 < 2)); // => false Console.WriteLine("2 <= 2? " + (2 <= 2)); // => true Console.WriteLine("2 >= 2? " + (2 >= 2)); // => true
位操做符
自增、自減
int i = 0; Console.WriteLine("\n->Inc/Dec-rementation"); Console.WriteLine(i++); //i = 1. 過後自增 Console.WriteLine(++i); //i = 2. 事先自增 Console.WriteLine(i--); //i = 1. 過後自減 Console.WriteLine(--i); //i = 0. 事先自減
Console.WriteLine("\n->Control Structures");
int j = 10; if (j == 10) { Console.WriteLine("I get printed"); } else if (j > 10) { Console.WriteLine("I don't"); } else { Console.WriteLine("I also don't"); }
簡單的 if/else 語句能夠寫成:
// <條件> ? <真> : <假> string isTrue = (true) ? "True" : "False";
int fooWhile = 0; while (fooWhile < 100) { //迭代 100 次, fooWhile 0->99 fooWhile++; }
int fooDoWhile = 0; do { //迭代 100 次, fooDoWhile 0->99 fooDoWhile++; } while (fooDoWhile < 100);
//for 循環結構 => for(<初始條件>; <條件>; <步>) for (int fooFor = 0; fooFor < 10; fooFor++) { //迭代10次, fooFor 0->9 }
foreach 循環結構 => foreach(<迭代器類型> <迭代器> in <可枚舉結構>)
foreach 循環適用於任何實現了 IEnumerable 或 IEnumerable
都實現了這些接口。下面的代碼中,ToCharArray()能夠刪除,由於字符串一樣實現了IEnumerable。
foreach (char character in "Hello World".ToCharArray()) { //迭代字符串中的全部字符 }
switch 適用於 byte、short、char和int 數據類型。一樣適用於可枚舉的類型,包括字符串類, 以及一些封裝了原始值的類:Character、Byte、Short和Integer。
int month = 3; string monthString; switch (month) { case 1: monthString = "January"; break; case 2: monthString = "February"; break; case 3: monthString = "March"; break; // 你能夠一次匹配多個case語句 // 可是你在添加case語句後須要使用break // (不然你須要顯式地使用goto case x語句) case 6: case 7: case 8: monthString = "Summer time!!"; break; default: monthString = "Some other month"; break; }
轉換字符串爲整數,轉換失敗會拋出異常:
int.Parse("123");//返回整數類型的"123"
TryParse會嘗試轉換類型,失敗時會返回缺省類型,例如 0
int tryInt; if (int.TryParse("123", out tryInt)) // Funciton is boolean Console.WriteLine(tryInt); // 123
轉換整數爲字符串
Convert類提供了一系列便利轉換的方法
Convert.ToString(123); // or tryInt.ToString(); }
public static void Classes() { // 參看文件尾部的對象聲明 // 使用new初始化對象 Bicycle trek = new Bicycle(); // 調用對象的方法 trek.SpeedUp(3); // 你應該一直使用setter和getter方法 trek.Cadence = 100; // 查看對象的信息. Console.WriteLine("trek info: " + trek.Info()); // 實例化一個新的Penny Farthing PennyFarthing funbike = new PennyFarthing(1, 10); Console.WriteLine("funbike info: " + funbike.Info()); Console.Read(); } // 結束main方法
終端程序 終端程序必須有一個main方法做爲入口
public static void Main(string[] args) { OtherInterestingFeatures(); }
public // 可見性 static // 容許直接調用類,無需先建立實例 int, //返回值 MethodSignatures( int maxCount, // 第一個變量,類型爲整型 int count = 0, // 若是沒有傳入值,則缺省值爲0 int another = 3, params string[] otherParams // 捕獲其餘參數 ) { return -1; }
方法能夠重名,只要簽名不同
public static void MethodSignature(string maxCount) { }
TKey和TValue類由用用戶調用函數時指定。
如下函數模擬了Python的SetDefault
public static TValue SetDefault<TKey, TValue>( IDictionary<TKey, TValue> dictionary, TKey key, TValue defaultItem) { TValue result; if (!dictionary.TryGetValue(key, out result)) return dictionary[key] = defaultItem; return result; }
你能夠限定傳入值的範圍
public static void IterateAndPrint<T>(T toPrint) where T: IEnumerable<int> { // 咱們能夠進行迭代,由於T是可枚舉的 foreach (var item in toPrint) // ittm爲整數 Console.WriteLine(item.ToString()); }
public static void OtherInterestingFeatures() {
MethodSignatures(3, 1, 3, "Some", "Extra", "Strings"); MethodSignatures(3, another: 3); // 顯式指定參數,忽略可選參數
int i = 3; i.Print(); // 參見下面的定義
可爲null的類型 對數據庫交互、返回值頗有用,任何值類型 (i.e. 不爲類) 添加後綴?
後會變爲可爲null的類型
// <類型>? <變量名> = <值> int? nullable = null; // Nullable<int> 的簡寫形式 Console.WriteLine("Nullable variable: " + nullable); bool hasValue = nullable.HasValue; // 不爲null時返回真 // ?? 是用於指定默認值的語法糖 // 以防變量爲null的狀況 int notNullable = nullable ?? 0; // 0
你可讓編譯器推斷變量類型:
var magic = "編譯器肯定magic是一個字符串,因此仍然是類型安全的"; // magic = 9; // 不工做,由於magic是字符串,而不是整數。
var phonebook = new Dictionary<string, string>() { {"Sarah", "212 555 5555"} // 在電話簿中加入新條目 };
調用上面定義爲泛型的SETDEFAULT
Console.WriteLine(SetDefault<string,string>(phonebook, "Shaun", "No Phone")); // 沒有電話
你不用指定TKey、TValue,由於它們會被隱式地推導出來
Console.WriteLine(SetDefault(phonebook, "Sarah", "No Phone")); // 212 555 5555
容許你用一行代碼搞定函數
Func<int, int> square = (x) => x * x; // 最後一項爲返回值 Console.WriteLine(square(3)); // 9
讓你很容易地處理未管理的資源。大多數訪問未管理資源 (文件操做符、設備上下文, etc.)的對象, 都實現了IDisposable接口。
using語句會爲你清理IDisposable對象。
using (StreamWriter writer = new StreamWriter("log.txt")) { writer.WriteLine("這裏沒有什麼可疑的東西"); // 在做用域的結尾,資源會被回收 // (即便有異常拋出,也同樣會回收) }
var websites = new string[] { "http://www.google.com", "http://www.reddit.com", "http://www.shaunmccarthy.com" }; var responses = new Dictionary<string, string>();
爲每一個請求新開一個線程,在運行下一步前合併結果
Parallel.ForEach(websites, new ParallelOptions() {MaxDegreeOfParallelism = 3}, // max of 3 threads website => { // Do something that takes a long time on the file using (var r = WebRequest.Create(new Uri(website)).GetResponse()) { responses[website] = r.ContentType; } });
直到全部的請求完成後纔會運行下面的代碼
foreach (var key in responses.Keys) Console.WriteLine("{0}:{1}", key, responses[key]);
配合其餘語言使用很方便
dynamic student = new ExpandoObject(); student.FirstName = "First Name"; // 不須要先定義類!
你甚至能夠添加方法(接受一個字符串,輸出一個字符串)
student.Introduce = new Func<string, string>( (introduceTo) => string.Format("Hey {0}, this is {1}", student.FirstName, introduceTo)); Console.WriteLine(student.Introduce("Beth"));
幾乎全部的集合都實現了它,帶給你 Map / Filter / Reduce 風格的方法
var bikes = new List<Bicycle>(); bikes.Sort(); // Sorts the array bikes.Sort((b1, b2) => b1.Wheels.CompareTo(b2.Wheels)); // 根據車輪數排序 var result = bikes .Where(b => b.Wheels > 3) // 篩選 - 能夠連鎖使用 (返回IQueryable) .Where(b => b.IsBroken && b.HasTassles) .Select(b => b.ToString()); // Map - 這裏咱們使用了select,因此結果是IQueryable<string> var sum = bikes.Sum(b => b.Wheels); // Reduce - 計算集合中的輪子總數
建立一個包含基於自行車的一些參數生成的隱式對象的列表
var bikeSummaries = bikes.Select(b=>new { Name = b.Name, IsAwesome = !b.IsBroken && b.HasTassles });
很難演示,可是編譯器在代碼編譯完成前就能推導出以上對象的類型
foreach (var bikeSummary in bikeSummaries.Where(b => b.IsAwesome)) Console.WriteLine(bikeSummary.Name);
邪惡的特性 —— 組合了linq和並行操做
var threeWheelers = bikes.AsParallel().Where(b => b.Wheels == 3).Select(b => b.Name);
以上代碼會併發地運行。會自動新開線程,分別計算結果。適用於多核、大數據量的場景。
將IQueryable<T>
映射到存儲,延緩執行,例如 LinqToSql 映射數據庫, LinqToXml 映射XML文檔。
var db = new BikeRespository();
執行被延遲了,這對於查詢數據庫來講很好
var filter = db.Bikes.Where(b => b.HasTassles); // 不運行查詢 if (42 > 6) // 你能夠不斷地增長篩選,包括有條件的篩選,例如用於「高級搜索」功能 filter = filter.Where(b => b.IsBroken); // 不運行查詢 var query = filter .OrderBy(b => b.Wheels) .ThenBy(b => b.Name) .Select(b => b.Name); // 仍然不運行查詢
如今運行查詢,運行查詢的時候會打開一個讀取器,因此你迭代的是一個副本
foreach (string bike in query) Console.WriteLine(result); } } // 結束LearnCSharp類
你能夠在同一個 .cs 文件中包含其餘類
public static class Extensions {
public static void Print(this object obj) { Console.WriteLine(obj.ToString()); } }
聲明類的語法:
// <public/private/protected/internal> class <類名>{ // //數據字段, 構造器, 內部函數. / // 在Java中函數被稱爲方法。 // } public class Bicycle { // 自行車的字段、變量 public int Cadence // Public: 任何地方均可以訪問 { get // get - 定義獲取屬性的方法 { return _cadence; } set // set - 定義設置屬性的方法 { _cadence = value; // value是被傳遞給setter的值 } } private int _cadence; protected virtual int Gear // 類和子類能夠訪問 { get; // 建立一個自動屬性,無需成員字段 set; } internal int Wheels // Internal:在同一程序集內能夠訪問 { get; private set; // 能夠給get/set方法添加修飾符 } int _speed; // 默認爲private: 只能夠在這個類內訪問,你也可使用`private`關鍵詞 public string Name { get; set; }
enum類型包含一組常量,它將名稱映射到值(除非特別說明,是一個整型)。enmu元素的類型能夠是byte、sbyte、short、ushort、int、uint、long、ulong。enum不能包含相同的值。
public enum BikeBrand { AIST, BMC, Electra = 42, //你能夠顯式地賦值 Gitane // 43 }
咱們在Bicycle類中定義的這個類型,因此它是一個內嵌類型。這個類之外的代碼應當使用Bicycle.Brand
來引用。
public BikeBrand Brand; // 聲明一個enum類型以後,咱們能夠聲明這個類型的字段
靜態方法的類型爲自身,不屬於特定的對象。你無需引用對象就能夠訪問他們。
// Console.WriteLine("Bicycles created: " + Bicycle.bicyclesCreated); static public int BicyclesCreated = 0;
只讀值在運行時肯定,它們只能在聲明或構造器內被賦值。
readonly bool _hasCardsInSpokes = false; // read-only private
構造器是建立類的一種方式。
下面是一個默認的構造器。
public Bicycle() { this.Gear = 1; // 你可使用關鍵詞this訪問對象的成員 Cadence = 50; // 不過你並不老是須要它 _speed = 5; Name = "Bontrager"; Brand = BikeBrand.AIST; BicyclesCreated++; }
另外一個構造器的例子(包含參數)
public Bicycle(int startCadence, int startSpeed, int startGear, string name, bool hasCardsInSpokes, BikeBrand brand) : base() // 首先調用base { Gear = startGear; Cadence = startCadence; _speed = startSpeed; Name = name; _hasCardsInSpokes = hasCardsInSpokes; Brand = brand; }
構造器能夠連鎖使用
public Bicycle(int startCadence, int startSpeed, BikeBrand brand) : this(startCadence, startSpeed, 0, "big wheels", true, brand) { }
// <public/private/protected> <返回值> <函數名稱>(<參數>)
類能夠爲字段實現 getters 和 setters 方法 for their fields,或者能夠實現屬性(C#推薦使用這個)。
方法的參數能夠有默認值。在有默認值的狀況下,調用方法的時候能夠省略相應的參數。
public void SpeedUp(int increment = 1) { _speed += increment; } public void SlowDown(int decrement = 1) { _speed -= decrement; }
屬性能夠訪問和設置值。當只須要訪問數據的時候,考慮使用屬性。屬性能夠定義get和set,或者是同時定義二者。
private bool _hasTassles; // private variable public bool HasTassles // public accessor { get { return _hasTassles; } set { _hasTassles = value; } }
你能夠在一行以內定義自動屬性,這個語法會自動建立後備字段。你能夠給getter或setter設置訪問修飾符,以便限制它們的訪問。
public bool IsBroken { get; private set; }
屬性的實現能夠是自動的
public int FrameSize { get; // 你能夠給get或set指定訪問修飾符 // 如下代碼意味着只有Bicycle類能夠調用Framesize的set private set; }
顯示對象屬性的方法
public virtual string Info() { return "Gear: " + Gear + " Cadence: " + Cadence + " Speed: " + _speed + " Name: " + Name + " Cards in Spokes: " + (_hasCardsInSpokes ? "yes" : "no") + "\n------------------------------\n" ; }
方法能夠是靜態的。一般用於輔助方法。
public static bool DidWeCreateEnoughBycles() { // 在靜態方法中,你只能引用類的靜態成員 return BicyclesCreated > 9000; } // 若是你的類只須要靜態成員,考慮將整個類做爲靜態類。 } // Bicycle類結束
PennyFarthing是Bicycle的一個子類
class PennyFarthing : Bicycle { // (Penny Farthings是一種前輪很大的自行車。沒有齒輪。) // 調用父構造器 public PennyFarthing(int startCadence, int startSpeed) : base(startCadence, startSpeed, 0, "PennyFarthing", true, BikeBrand.Electra) { } protected override int Gear { get { return 0; } set { throw new ArgumentException("你不可能在PennyFarthing上切換齒輪"); } } public override string Info() { string result = "PennyFarthing bicycle "; result += base.ToString(); // 調用父方法 return result; } }
接口只包含成員的簽名,而沒有實現。
interface IJumpable { void Jump(int meters); // 全部接口成員是隱式地公開的 } interface IBreakable { bool Broken { get; } // 接口能夠包含屬性、方法和事件 }
類只能繼承一個類,可是能夠實現任意數量的接口
class MountainBike : Bicycle, IJumpable, IBreakable { int damage = 0; public void Jump(int meters) { damage += meters; } public bool Broken { get { return damage > 100; } } }
鏈接數據庫,一個 LinqToSql的示例。EntityFramework Code First 很棒 (相似 Ruby的 ActiveRecord, 不過是雙向的)
http://msdn.microsoft.com/en-us/data/jj193542.aspx
public class BikeRespository : DbSet { public BikeRespository() : base() { } public DbSet<Bicycle> Bikes { get; set; } } } // 結束 Namespace