代碼如下:
// 單行注釋以 // 開始
/*
多行注釋是這樣的
*/
/// <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)
sbyte fooSbyte = 100;
// Byte - 無符號 8-bit 整數
// (0 <= byte <= 255)
byte fooByte = 100;
// Short - 16-bit 整數
// 有符號 - (-32,768 <= short <= 32,767)
// 無符號 - (0 <= ushort <= 65,535)
short fooShort = 10000;
ushort fooUshort = 10000;
// Integer - 32-bit 整數
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';
// Strings -- 和前面的基本類型不同,字符串不是值,而是引用。這意味著你可以將字符串設為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;
///////////////////////////////////////////////////
// 數據結構
///////////////////////////////////////////////////
// 數組 - 從0開始計數,聲明數組時需要確定數組長度。
// 聲明數組的格式如下:
// <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 }; // intialize
// <>用于泛型 - 參考下文
// 列表無默認值,訪問列表元素時必須首先添加元素
intList.Add(1);
Console.WriteLine("intList @ 0: " + intList[0]);
// 其他數據結構:
// 堆棧/隊列
// 字典 (哈希表的實現)
// 哈希集合
// 只讀集合
// 元組 (.Net 4+)
///////////////////////////////////////
// 操作符
///////////////////////////////////////
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");
// 類似C的if語句
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 語句可以寫成:
// <condition> ? <true> : <false>
string isTrue = (true) ? "True" : "False";
// While 循環
int fooWhile = 0;
while (fooWhile < 100)
{
//迭代 100 次, fooWhile 0->99
fooWhile++;
}
// Do While 循環
int fooDoWhile = 0;
do
{
//迭代 100 次, fooDoWhile 0->99
fooDoWhile++;
} while (fooDoWhile < 100);
//for 循環
//for 循環結構 => for(<初始條件>; <條件>; <步>)
for (int fooFor = 0; fooFor < 10; fooFor++)
{
//迭代10次, fooFor 0->9
}
// foreach循環
// foreach 循環結構 => foreach(<迭代器類型> <迭代器> in <可枚舉結構>)
// foreach 循環適用于任何實現了 IEnumerable 或 IEnumerable 的對象。
// .Net 框架下的集合類型(數組, 列表, 字典...)都實現了這些接口。
// 下面的代碼中,ToCharArray()可以刪除,因為字符串同樣實現了IEnumerable。
foreach (char character in "Hello World".ToCharArray())
{
//迭代字符串中的所有字符
}
// Switch 語句
// 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;
}
///////////////////////////////////////
// 轉換字符串為整數,轉換失敗會拋出異常:
///////////////////////////////////////
// Converting data
// 轉換字符串為整數,轉換失敗會拋出異常:
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 is a string, at compile time, so you still get type safety";
// 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
// lambda表達式 - 允許你用一行代碼搞定函數
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("這里沒有什么可疑的東西");
// 在作用域的結尾,資源會被回收
// (即使有異常拋出,也一樣會回收)
}
// 并行框架
// http://blogs.msdn.com/b/csharpfaq/archive/2010/06/01/parallel-programming-in-net-framework-4-getting-started.aspx
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"));
// IQUERYABLE<T> - 幾乎所有的集合都實現了它,帶給你 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);
// ASPARALLEL
// 邪惡的特性 —— 組合了linq和并行操作
var threeWheelers = bikes.AsParallel().Where(b => b.Wheels == 3).Select(b => b.Name);
// 以上代碼會并發地運行。會自動新開線程,分別計算結果。適用于多核、大數據量的場景。
// LINQ - 將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類型之后,我們可以聲明這個類型的字段
// 靜態方法
// 靜態方法的類型為自身,不屬于特定的對象。你無需引用對象就可以訪問他們。
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;
}
}
}
/// <summary>
/// 連接數據庫,一個 LinqToSql的示例。EntityFramework Code First 很棒 (類似 Ruby的 ActiveRecord, 不過是雙向的)
/// http://msdn.microsoft.com/en-us/data/jj193542.aspx
/// </summary>
public class BikeRespository : DbSet
{
public BikeRespository()
: base()
{
}
public DbSet<Bicycle> Bikes { get; set; }
}
} // 結束 Namespace
注意,沒有涉及到的主題有:
1.Flags
2.Attributes
3.靜態屬性
4.Exceptions, Abstraction
6.ASP.NET (Web Forms/MVC/WebMatrix)
6.Winforms
7.Windows Presentation Foundation (WPF)