高级C#
使用delegates委托写插件方法:
public delegate int Transformer (int x); public class Util
{
public static void Transform (int[] values, Transformer t)
{
for (int i = 0; i < values.Length; i++)
values[i] = t(values[i]);
}
} class Test
{
static void Main()
{
int[] values = new int[] {1, 2, 3};
Util.Transform(values, Square); // dynamically hook in Square
foreach (int i in values)
Console.Write (i + " "); // 1 4 9
} static int Square (int x) { return x * x; }
}
多重delegate委托例子:
public delegate void ProgressReporter (int percentComplete); public class Util
{
public static void HardWork (ProgressReporter p)
{
for (int i = 0; i < 10; i++)
{
p (i * 10); // Invoke delegate
System.Threading.Thread.Sleep(100); // Simulate hard work
}
}
}
class Test
{
static void Main ()
{
ProgressReporter p = WriteProgressToConsole;
p += WriteProgressToFile;
Util.HardWork (p);
} static void WriteProgressToConsole (int percentComplete)
{
Console.WriteLine (percentComplete);
} static void WriteProgressToFile (int percentComplete)
{
System.IO.File.WriteAllText ("progress.txt", percentComplete.ToString());
}
}
实例方法目标:
public delegate void ProgressReporter (int percentComplete); class Test
{
static void Main() {new Test();}
Test ()
{
ProgressReporter p = InstanceProgress;
p(99); // 99
Console.WriteLine (p.Target == this); // True
Console.WriteLine (p.Method); // Void InstanceProgress(Int32)
} void InstanceProgress (int percentComplete)
{
Console.WriteLine(percentComplete);
}
}
泛Delegate委托类型:
public delegate T Transformer<T> (T arg); public class Util
{
public static void Transform<T> (T[] values, Transformer<T> t)
{
for (int i = 0; i < values.Length; i++)
values[i] = t(values[i]);
}
} class Test
{
static void Main()
{
int[] values = new int[] {1, 2, 3};
Util.Transform(values, Square); // dynamically hook in Square
foreach (int i in values)
Console.Write (i + " "); // 1 4 9
} static int Square (int x) { return x * x; }
}
Delegates委托和Interfaces接口:
public interface ITransformer
{
int Transform (int x);
} public class Util
{
public static void TransformAll (int[] values, ITransformer t)
{
for (int i = 0; i < values.Length; i++)
values[i] = t.Transform(values[i]);
}
} class Test : ITransformer
{
static void Main()
{
int[] values = new int[] {1, 2, 3};
Util.TransformAll(values, new Test());
foreach (int i in values)
Console.WriteLine (i);
} public int Transform (int x) { return x * x; }
}
class Test
{
static void Main()
{
int[] values = new int[] {1, 2, 3};
Util.TransformAll(values, new Cuber());
foreach (int i in values)
Console.WriteLine (i);
} class Squarer : ITransformer
{
public int Transform (int x) { return x * x; }
}
class Cuber : ITransformer
{
public int Transform (int x) {return x * x * x; }
}
}
Delegate委托参数兼容 (逆变):
delegate void SpecificDelegate (SpecificClass s); class SpecificClass {} class Test
{
static void Main()
{
SpecificDelegate specificDelegate = GeneralHandler;
specificDelegate (new SpecificClass());
} static void GeneralHandler(object o)
{
Console.WriteLine(o.GetType()); // SpecificClass
}
}
Delegate委托返回类型兼容 (逆变):
delegate Asset DebtCollector(); class Asset {} class House : Asset {} class Test
{
static void Main()
{
DebtCollector d = new DebtCollector (GetHomeSweetHome);
Asset a = d();
Console.WriteLine(a.GetType()); // House
}
static House GetHomeSweetHome() {return new House(); }
}
Events事件:
public delegate void PriceChangedHandler (decimal oldPrice,
decimal newPrice); public class Stock
{
string symbol;
decimal price; public Stock (string symbol) {this.symbol = symbol;} public event PriceChangedHandler PriceChanged; public decimal Price
{
get { return price; }
set
{
if (price == value) return; // exit if nothing has changed
if (PriceChanged != null) // if invocation list not empty
PriceChanged (price, value); // fire event
price = value;
}
}
}
标准Event事件模式:
using System; public class PriceChangedEventArgs : EventArgs
{
public readonly decimal LastPrice;
public readonly decimal NewPrice; public PriceChangedEventArgs (decimal lastPrice, decimal newPrice)
{
LastPrice = lastPrice; NewPrice = newPrice;
}
} public class Stock
{
string symbol;
decimal price; public Stock (string symbol) {this.symbol = symbol;} public event EventHandler<PriceChangedEventArgs> PriceChanged; protected virtual void OnPriceChanged (PriceChangedEventArgs e)
{
if (PriceChanged != null) PriceChanged (this, e);
} public decimal Price
{
get { return price; }
set
{
if (price == value) return;
OnPriceChanged (new PriceChangedEventArgs (price, value));
price = value;
}
}
} class Test
{
static void Main()
{
Stock stock = new Stock ("THPW");
stock.Price = 27.10M;
// register with the PriceChanged event
stock.PriceChanged += stock_PriceChanged;
stock.Price = 31.59M;
} static void stock_PriceChanged (object sender, PriceChangedEventArgs e)
{
if ((e.NewPrice - e.LastPrice) / e.LastPrice > 0.1M)
Console.WriteLine ("Alert, 10% stock price increase!");
}
}
使用EventArgs.Empty:
public class Stock
{
string symbol;
decimal price; public Stock (string symbol) {this.symbol = symbol;} public event EventHandler PriceChanged; protected virtual void OnPriceChanged (EventArgs e)
{
if (PriceChanged != null) PriceChanged (this, e);
} public decimal Price
{
get { return price; }
set
{
if (price == value) return;
price = value;
OnPriceChanged (EventArgs.Empty);
}
}
}
Event存取:
public interface IFoo
{
event EventHandler Ev;
} class Foo : IFoo
{
private EventHandler ev; event EventHandler IFoo.Ev
{
add { ev += value; }
remove { ev -= value; }
}
}
Lambda表达式:
delegate int Transformer (int i); class Test
{
static void Main()
{
Transformer square = x => x * x;
Console.WriteLine (square(3)); // 9
}
}
捕获外部变量(闭包):
delegate int NumericSequence (); class Test
{
static void Main()
{
int seed = 0;
NumericSequence natural = () => seed++;
Console.WriteLine (natural()); // 0
Console.WriteLine (natural()); // 1
}
}
delegate int NumericSequence (); class Test
{
static NumericSequence Natural ()
{
int seed = 0; // executes once (per call to Natural())
return () => seed++; // executes twice (per call to delegate instance
// returned by Natural())
} static void Main()
{
NumericSequence natural = Natural ();
Console.WriteLine (natural()); // 0
Console.WriteLine (natural()); // 1
}
}
delegate int NumericSequence (); class Test
{
static NumericSequence Natural ()
{
return () => {int seed = 0; return seed++; };
} static void Main()
{
NumericSequence natural = Natural ();
Console.WriteLine (natural()); // 0
Console.WriteLine (natural()); // 0
}
}
匿名方法:
delegate int Transformer (int i); class Test
{
static void Main()
{
Transformer square = delegate (int x) {return x * x;};
Console.WriteLine (square(3)); // 9
}
}
Try语句和异常:
class Test
{
static int Calc (int x) {return 10 / x;} static void Main()
{
try
{
int y = Calc (0);
Console.WriteLine (y);
}
catch (DivideByZeroException ex)
{
Console.WriteLine("x cannot be zero");
}
Console.WriteLine ("program completed");
}
}
多Catch语句:
class Test
{
static void Main (string[] args)
{
try
{
byte b = byte.Parse (args[0]);
Console.WriteLine (b);
}
catch (IndexOutOfRangeException ex)
{
Console.WriteLine ("Please provide at least one argument");
}
catch (FormatException ex)
{
Console.WriteLine ("That's not a number!");
}
catch (OverflowException ex)
{
Console.WriteLine ("You've given me more than a byte!");
}
}
}
Finally语句块:
using System;
using System.IO; class Test
{
static void Main ()
{
StreamReader reader = null;
try
{
reader = File.OpenText ("file.txt");
if (reader.EndOfStream) return;
Console.WriteLine (reader.ReadToEnd ());
}
finally
{
if (reader != null) reader.Dispose ();
}
}
使用语句:
StreamReader reader = File.OpenText ("file.txt");
try
{
// ...
}
finally
{
if (reader != null)
((IDisposable)reader).Dispose();
}
抛出异常:
class Test
{
static void Display (string name)
{
if (name == null)
throw new ArgumentNullException ("name"); Console.WriteLine (name);
} static void Main()
{
try { Display (null); }
catch (ArgumentNullException ex)
{
Console.WriteLine ("Caught the exception");
}
}
}
重新抛出:
string s; using (WebClient wc = new WebClient())
try { s = wc.DownloadString ("http://albahari.com/"); }
catch (WebException ex)
{
if (ex.Status == WebExceptionStatus.NameResolutionFailure)
Console.WriteLine ("Bad domain name");
else
throw; // Can't handle other sorts of WebException, so rethrow
}
原子模式:
class Test
{
static void Main()
{
Accumulator a = new Accumulator ();
try
{
a.Add (4, 5); // a.Total is now 9
a.Add (1, int.MaxValue); // will cause OverflowException
}
catch (OverflowException)
{
Console.WriteLine (a.Total); // a.Total is still 9
}
}
}
public class Accumulator
{
public int Total; public void Add(params int[] ints)
{
bool success = false;
int totalSnapshot = Total;
try
{
foreach (int i in ints)
{
checked
{
Total += i;
}
}
success = true;
}
finally
{
if (! success)
Total = totalSnapshot;
}
}
}
迭代器:
using System;
using System.Collections.Generic; class Test
{
static void Main()
{
foreach (int fib in Fibs(6))
Console.Write (fib + " ");
} static IEnumerable<int> Fibs(int fibCount)
{
for (int i = 0, prevFib = 1, curFib = 1; i < fibCount; i++)
{
yield return prevFib;
int newFib = prevFib+curFib;
prevFib = curFib;
curFib = newFib;
}
}
}
多yield语句:
class Test
{
static void Main()
{
foreach (string s in Foo())
Console.WriteLine(s); // prints "One","Two","Three"
} static IEnumerable<string> Foo()
{
yield return "One";
yield return "Two";
yield return "Three";
}
}
Yield break:
static IEnumerable<string> Foo(bool breakEarly)
{
yield return "One";
yield return "Two"; if (breakEarly)
yield break; yield return "Three";
}
构成序列:
using System;
using System.Collections.Generic; class Test
{
static void Main()
{
foreach (int fib in EvenNumbersOnly(Fibs(6)))
Console.WriteLine(fib);
} static IEnumerable<int> Fibs(int fibCount)
{
for (int i = 0, prevFib = 1, curFib = 1; i < fibCount; i++)
{
yield return prevFib;
int newFib = prevFib+curFib;
prevFib = curFib;
curFib = newFib;
}
} static IEnumerable<int> EvenNumbersOnly(IEnumerable<int> sequence)
{
foreach(int x in sequence)
if ((x % 2) == 0)
yield return x;
}
}
Nullable 类型: 操作升降:
int? x = 5;
int? y = null; // equality operator examples
Console.WriteLine(x == y); // false
Console.WriteLine(x == null); // false
Console.WriteLine(x == 5); // true
Console.WriteLine(y == null); // true
Console.WriteLine(y == 5); // false
Console.WriteLine(y != 5); // true // relational operator examples
Console.WriteLine(x < 6); // true
Console.WriteLine(y < 6); // false
Console.WriteLine(y > 6); // false // all other operator examples
Console.WriteLine(x + 5); // 10
Console.WriteLine(x + y); // null (prints empty line)
bool? 语义:
bool? n = null;
bool? f = false;
bool? t = true;
Console.WriteLine (n | n); // (null)
Console.WriteLine (n | f); // (null)
Console.WriteLine (n | t); // True
Console.WriteLine (n & n); // (null)
Console.WriteLine (n & f); // False
Console.WriteLine (n & t); // (null)
环境属性:
public class Row
{
// ...
Grid parent;
Color? backColor; public Color BackColor
{
get { return backColor ?? parent.BackColor; }
set { backColor = backColor == parent.BackColor ? null : value; }
}
}
操作符重载:
public struct Note
{
int value;
public Note (int semitonesFromA) { value = semitonesFromA; } public static Note operator + (Note x, int semitones)
{
return new Note (x.value + semitones);
}
}
自定义转换:
// Convert to hertz
public static implicit operator double(Note x)
{
return 440 * Math.Pow (2,(double) x.value / 12 );
} // Convert from hertz (only accurate to nearest semitone)
public static explicit operator Note(double x)
{
return new Note ((int) (0.5 + 12 * (Math.Log(x/440) / Math.Log(2)) ));
}
重载true和false:
class Test
{
static void Main()
{
SqlBoolean a = SqlBoolean.Null;
if (a)
Console.WriteLine("True");
else if (! a)
Console.WriteLine("False");
else
Console.WriteLine("Null");
}
}
public struct SqlBoolean
{
public static bool operator true (SqlBoolean x)
{
return x.m_value == True.m_value;
} public static bool operator false (SqlBoolean x)
{
return x.m_value == False.m_value;
} public static SqlBoolean operator !(SqlBoolean x)
{
if (x.m_value == Null.m_value) return Null;
if (x.m_value == False.m_value) return True;
return False;
} public static readonly SqlBoolean Null = new SqlBoolean(0);
public static readonly SqlBoolean False = new SqlBoolean(1);
public static readonly SqlBoolean True = new SqlBoolean(2); private SqlBoolean (byte value) {m_value = value;}
private byte m_value;
}
扩展方法:
public static class StringHelper
{
public static bool IsCapitalized (this string s)
{
if (string.IsNullOrEmpty(s)) return false;
return char.IsUpper(s[0]);
}
}
在接口上扩展方法:
using System;
using System.Collections.Generic; static class Test
{
static void Main()
{
var strings = new string[] { "a", "b", null, "c"};
foreach (string s in strings.StripNulls())
Console.WriteLine(s);
} static IEnumerable<T> StripNulls<T> (this IEnumerable<T> seq)
{
foreach (T t in seq)
if (t != null)
yield return t;
}
}
不安全代码:
unsafe void RedFilter(int[,] bitmap)
{
int length = bitmap.Length;
fixed (int* b = bitmap)
{
int* p = b;
for(int i = 0; i < length; i++)
*p++ &= 0xFF;
}
}
fixed语句:
class Test
{
int x;
static void Main()
{
Test test = new Test ();
unsafe
{
fixed(int* p = &test.x) // pins test
{
*p = 9;
}
System.Console.WriteLine(test.x);
}
}
}
成员指向操作符:
struct Test
{
int x;
unsafe static void Main()
{
Test test = new Test();
Test* p = &test;
p->x = 9;
System.Console.WriteLine(test.x);
}
}
stackalloc:
int* a = stackalloc int [10];
for (int i = 0; i < 10; ++i)
Console.WriteLine(a[i]); // print raw memory
Fixed-size buffers:
unsafe struct UnsafeUnicodeString
{
public short Length;
public fixed byte Buffer[30];
} unsafe class UnsafeClass
{
private UnsafeUnicodeString uus;
public UnsafeClass (string s)
{
uus.Length = (short)s.Length;
fixed (byte* p = uus.Buffer)
for (int i = 0; i < s.Length; i++)
p[i] = (byte)s[i];
}
} class Test
{
void*:
class Test
{
unsafe static void Main ()
{
short[ ] a = {1,1,2,3,5,8,13,21,34,55};
fixed (short* p = a)
{
//sizeof returns size of value-type in bytes
Zap (p, a.Length * sizeof (short));
}
foreach (short x in a)
System.Console.WriteLine (x); // prints all zeros
} unsafe static void Zap (void* memory, int byteCount)
{
byte* b = (byte*)memory;
for (int i = 0; i < byteCount; i++)
*b++ = 0;
}
}
预处理器指令:
#define DEBUG
class MyClass
{
int x;
void Foo()
{
# if DEBUG
Console.WriteLine("Testing: x = {0}", x);
# endif
}
...
}
条件属性:
// file1.cs
#define DEBUG
using System;
using System.Diagnostics; [Conditional("DEBUG")]
public class TestAttribute : Attribute {} // file2.cs
#define DEBUG
[Test]
class Foo
{
[Test]
private string s;
}
#pragma warning:
public class Foo
{
static void Main() { } #pragma warning disable 414
static string Message = "Hello";
#pragma warning restore 414
}
XML文档:
using System; class Test
{
/// <summary>
/// The Foo method is called from
/// <see cref="Main">Main</see>
/// </summary>
/// <mytag>user defined tag info</mytag>
/// <param name="s">Description for s</param>
static void Foo(string s) { Console.WriteLine(s); } static void Main() { Foo("42"); }
}
namespace NS
{
/// T:NS.MyClass
class MyClass
{
/// F:NS.MyClass.aField
string aField; /// P:NS.MyClass.aProperty
short aProperty {get {...} set {...}} /// T:NS.MyClass.NestedType
class NestedType {...}; /// M:NS.MyClass.X()
void X() {...} /// M:NS.MyClass.Y(System.Int32,System.Double@,System.Decimal@)
void Y(int p1, ref double p2, out decimal p3) {...} /// M:NS.MyClass.Z(System.Char[ ],System.Single[0:,0:])
void Z(char[ ] 1, float[,] p2) {...} /// M:NS.MyClass.op_Addition(NS.MyClass,NS.MyClass)
public static MyClass operator+(MyClass c1, MyClass c2) {...} /// M:NS.MyClass.op_Implicit(NS.MyClass)~System.Int32
public static implicit operator int(MyClass c) {...} /// M:NS.MyClass.#ctor
MyClass() {...} /// M:NS.MyClass.Finalize
~MyClass() {...} /// M:NS.MyClass.#cctor
static MyClass() {...}
}
}
高级C#的更多相关文章
- MySQL高级知识- MySQL的架构介绍
[TOC] 1.MySQL 简介 概述 MySQL是一个关系型数据库管理系统,由瑞典MySQL AB公司开发,目前属于Oracle公司. MySQL是一种关联数据库管理系统,将数据保存在不同的表中,而 ...
- PayPal高级工程总监:读完这100篇论文 就能成大数据高手(附论文下载)
100 open source Big Data architecture papers for data professionals. 读完这100篇论文 就能成大数据高手 作者 白宁超 2016年 ...
- 马哥linux运维初级+中级+高级 视频教程 教学视频 全套下载(近50G)
马哥linux运维初级+中级+高级 视频教程 教学视频 全套下载(近50G)目录详情:18_02_ssl协议.openssl及创建私有CA18_03_OpenSSH服务及其相关应用09_01_磁盘及文 ...
- JS高级前端开发群加群说明及如何晋级
JS高级前端开发群加群说明 一.文章背景: 二. 高级群: 三. 加入方式: 四. 说明: 一.文章背景: 去年年初建了几个群,在不经意间火了,一直排在“前端开发”关键字搜索结果第一名.当然取得这 ...
- C#高级知识点&(ABP框架理论学习高级篇)——白金版
前言摘要 很早以前就有要写ABP高级系列教程的计划了,但是迟迟到现在这个高级理论系列才和大家见面.其实这篇博客很早就着手写了,只是楼主一直写写停停.看看下图,就知道这篇博客的生产日期了,谁知它的出厂日 ...
- Visual Studio 宏的高级用法
因为自 Visual Studio 2012 开始,微软已经取消了对宏的支持,所以本篇文章所述内容只适用于 Visual Studio 2010 或更早期版本的 VS. 在上一篇中,我已经介绍了如何编 ...
- [译]基于GPU的体渲染高级技术之raycasting算法
[译]基于GPU的体渲染高级技术之raycasting算法 PS:我决定翻译一下<Advanced Illumination Techniques for GPU-Based Volume Ra ...
- 高薪诚聘熟悉ABP框架的.NET高级开发工程师(2016年7月28日重发)
招聘单位是ABP架构设计交流群(134710707)群主阳铭所在的公司-上海运图贸易有限公司 招聘岗位:.NET高级开发工程师工作地点:上海-普陀区 [公司情况]上海运图贸易有限公司,是由易迅网的创始 ...
- [Java面经]干货整理, Java面试题(覆盖Java基础,Java高级,JavaEE,数据库,设计模式等)
如若转载请注明出处: http://www.cnblogs.com/wang-meng/p/5898837.html 谢谢.上一篇发了一个找工作的面经, 找工作不宜, 希望这一篇的内容能够帮助到大 ...
- MongoDB高级查询详细
前言 前几篇,老玩家绕道即可,新手晚上闲着也是蛋疼,不如把命令敲一边,这样你就会对MongoDB有一定的掌握啦.如果没有安装MongoDB去看我的上一篇博客 MongoDB下载安装与简单增删改查 前 ...
随机推荐
- 让你系统认识flume及安装和使用flume1.5传输数据到hadoop2.2
本文链接: http://www.aboutyun.com/thread-7949-1-1.html 问题导读:1.什么是flume?2.如何安装flume?3.flume的配置文件与其它软件有什么不 ...
- HW6.29
public class Solution { public static void main(String[] args) { int count = 0; int[] card = new int ...
- HDU-4665 Unshuffle 搜索 | 2-SAT
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4665 本题的2-SAT建图颇为复杂,有时间再来填坑(自己写的一直挂着,标程建图太复杂了)...然后用暴 ...
- sqlite 修改表名,合并数据库(文件)
修改表名:ALTER TABLE orig_table_name RENAME TO tmp_table_name; 将某个数据库的一个表的数据插入到另一个数据库的某个表里:1.先连接数据库A2.再a ...
- 纯CSS3实现的图片滑块程序,效果非常酷
接下来我们一起来分析一下源码,首先是HTML代码,非常简单: <div id="gal"> <nav class="galnav"> & ...
- html标签应用
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- SMP和MAPP的区别
SMP(Symmetrical Multi-Processing),对称多处理系统,是指在一个计算机上汇集了一组处理器(多CPU),各CPU之间共享内存子系统以及总线结构.它是相对非对称多处理技术而言 ...
- 让Visual Studio 2015 支持ASP.NET MVC4.0.0.1
近日装上了Visual Studio 2015 ,打开之前vs2013创建的MVC4的项目发现无法编译通过,提示System.Web.MVC,System.Web.WebPages 等找不到,网上搜索 ...
- java中的AES 256算法遇到 Illegal key size or default parameters错的解决办法
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html 内部邀请码:C8E245J (不写邀请码,没有现金送) 国 ...
- 连载:面向对象葵花宝典:思想、技巧与实践(32) - LSP原则
LSP是唯一一个以人名命名的设计原则,并且作者还是一个"女博士" ======================================================== ...