xiacy

导航

2012年5月29日 #

8.2 简化的初始化

摘要: public class Person{ public int Age { get; set; } public string Name { get; set; } List<Person> friends = new List<Person>(); public List<Person> Friends { get { return friends; } } Location home = new Location(); public Location Home { get { return home; } } public Person() { }... 阅读全文

posted @ 2012-05-29 23:22 xiacy 阅读(184) 评论(0) 推荐(0) 编辑

2012年5月14日 #

6.2.3 迭代器的执行流程(yield break 语句)

摘要: static IEnumerable<int> CountWithTimeLimit(DateTime limit){ for (int i = 1; i <= 100; i++) { if (DateTime.Now >= limit) yield break; yield return i; }}static void Main(string[] args){ DateTime stop = DateTime.Now.AddSeconds(2); foreach (int i in CountWithTimeL... 阅读全文

posted @ 2012-05-14 00:13 xiacy 阅读(194) 评论(0) 推荐(0) 编辑

2012年5月10日 #

6.2.2 观察迭代器的工作流程

摘要: static readonly string Padding = new string(' ', 30);static IEnumerable<int> CreateEnumerable(){ Console.WriteLine("{0}start of CreateEnumerable()", Padding); for (int i = 0; i < 3; i++) { Console.WriteLine("{0}about to yield {1}", Padding, i); yield return i; Co 阅读全文

posted @ 2012-05-10 22:52 xiacy 阅读(142) 评论(0) 推荐(0) 编辑

6.2.1 迭代器块和 yield return 简介

摘要: 利用c#2和 yield return 来迭代示例集合class Program{ static void Main(string[] args) { object[] values = { "a", "b", "c", "d", "e" }; IterationSample collection = new IterationSample(values, 1); foreach (object x in collection) { Console.WriteLine(x); } }}... 阅读全文

posted @ 2012-05-10 22:30 xiacy 阅读(269) 评论(0) 推荐(0) 编辑

2012年5月6日 #

5.5.6 共享和非共享的变量混合使用

摘要: MethodInvoker[] delegates = new MethodInvoker[2];int outside = 0;for(int i=0;i<2;i++){ int inside = 0; delegates[i] = delegate { Console.WriteLine("【outside:{0},inside:{1}】", outside, inside); outside++; inside++; };}MethodInvoker first=delegates[0];MethodInvoker sec... 阅读全文

posted @ 2012-05-06 12:36 xiacy 阅读(138) 评论(0) 推荐(0) 编辑

5.5.5 局部变量的实例化

摘要: List<MethodInvoker> list = new List<MethodInvoker>();for (int i = 0; i < 5; i++){ int counter = i * 10; list.Add(delegate { Console.WriteLine(counter); counter++; });}foreach (MethodInvoker t in list){ t();}list[0]();list[0]();list[0]();list[1]();输出结果01020304012311 阅读全文

posted @ 2012-05-06 12:07 xiacy 阅读(149) 评论(0) 推荐(0) 编辑

5.5.4 捕获变量的延长生存期

摘要: static void Main(string[] args){ MethodInvoker x = CreateDelegateInstance(); x(); x(); x();}static MethodInvoker CreateDelegateInstance(){ int counter = 5; MethodInvoker ret = delegate { Console.WriteLine(counter); counter++; }; ret(); return ret;}输出结果为:56... 阅读全文

posted @ 2012-05-06 12:00 xiacy 阅读(189) 评论(0) 推荐(0) 编辑

5.5.2 测试被捕获的变量的行为

摘要: string captured = "before x is created";MethodInvoker x = delegate{ Console.WriteLine(captured); captured = "changed by x";};Console.WriteLine(captured);captured = "directly before x is invoked";x();Console.WriteLine(captured);captured = "before second invocation&q 阅读全文

posted @ 2012-05-06 11:54 xiacy 阅读(138) 评论(0) 推荐(0) 编辑

5.5.1 定义闭包和不同类型

摘要: int outerVariable = 5;string capturedVariable = "captured";if (DateTime.Now.Hour == 23){ int normalLocalVariable = DateTime.Now.Minute; Console.WriteLine(normalLocalVariable);}MethodInvoker x = delegate(){ string anonLocal = "local to anonymous method"; Console.WriteLine("{0 阅读全文

posted @ 2012-05-06 11:46 xiacy 阅读(158) 评论(0) 推荐(0) 编辑

5.4.2 匿名方法的返回值

摘要: 从匿名方法放回一个值Predicate<int> isEven = delegate(int n) { return n % 2 == 0; };Console.WriteLine(isEven(1));Console.WriteLine(isEven(2));Console.WriteLine(isEven(3));Console.WriteLine(isEven(4));Console.WriteLine(isEven(5));Console.ReadKey();用匿名方法简便的排序文件static void SortAndShowFiles(string titile, Co 阅读全文

posted @ 2012-05-06 11:01 xiacy 阅读(256) 评论(0) 推荐(0) 编辑

2012年5月1日 #

5.4.1 将匿名方法用于Action<T>委托类型

摘要: Action<string> printReverse = delegate(string text) { char[] chars = text.ToCharArray(); Array.Reverse(chars); Console.WriteLine(new string(chars)); }; Action<int> printRoot = delegate(int number) { ... 阅读全文

posted @ 2012-05-01 22:57 xiacy 阅读(270) 评论(0) 推荐(0) 编辑

5.3.3 C#1和C#2之间的一处重大改变

摘要: delegate void SampleDelegate(string x); public class SinPpet { public void CandidateAction(string x) { Console.WriteLine("Snippet.CandidateAction"); } } public class Derived : SinPpet { public void CandidateAction(object o) { ... 阅读全文

posted @ 2012-05-01 22:23 xiacy 阅读(189) 评论(0) 推荐(0) 编辑

5.3.2 委托返回类型的协变性

摘要: delegate Stream StreamFactory(); static MemoryStream GenerateSampleData() { byte[] buffer = new byte[16]; for (int i = 0; i < buffer.Length; i++) { buffer[i] = (byte)i; } return new MemoryStream(buffer); ... 阅读全文

posted @ 2012-05-01 22:13 xiacy 阅读(151) 评论(0) 推荐(0) 编辑

5.2.1 委托参数的逆变性

摘要: static void LogPlainEvent(object sender, EventArgs e) { Console.WriteLine("LogPlain"); } static void LogKeyEvent(object sender, KeyPressEventArgs e) { Console.WriteLine("LogKey"); } static void LogMouseEvent(object sender, MouseEventArg... 阅读全文

posted @ 2012-05-01 22:12 xiacy 阅读(163) 评论(0) 推荐(0) 编辑

5.1.1 订阅三个按钮事件

摘要: static void LogPlainEvent(object sender, EventArgs e) { Console.WriteLine("LogPlain"); } static void LogKeyEvent(object sender, KeyPressEventArgs e) { Console.WriteLine("LogKey"); } static void LogMouseEvent(object sender, Mouse... 阅读全文

posted @ 2012-05-01 21:55 xiacy 阅读(159) 评论(0) 推荐(0) 编辑

4.3.2 使用null进行赋值和比较

摘要: class Person { DateTime birth; DateTime? death; string name; public TimeSpan Age { get { if (death.HasValue) return death.Value - birth; else return DateTime.Now - birth... 阅读全文

posted @ 2012-05-01 15:16 xiacy 阅读(186) 评论(0) 推荐(0) 编辑

4.2.1 使用Nullable<T>的各个成员

摘要: static void Display(Nullable<int> x) { Console.WriteLine("HasValue:{0}", x.HasValue); if (x.HasValue) { Console.WriteLine("Value:{0}", x.Value); Console.WriteLine("Explicit conversion:{0}", (int)x); } ... 阅读全文

posted @ 2012-05-01 14:38 xiacy 阅读(209) 评论(0) 推荐(0) 编辑

3.4.3 一个完整的泛型枚举---从0枚举到9

摘要: class CountingEnumerable : IEnumerable<int> { #region IEnumerable<int> 成员 public IEnumerator<int> GetEnumerator() { return new CountingEnumerator(); } #endregion #region IEnumerable 成员 IEnumerator IEnumerable.GetEnumerator() {... 阅读全文

posted @ 2012-05-01 13:57 xiacy 阅读(183) 评论(0) 推荐(0) 编辑

3.4.2 泛型类型的静态构造函数

摘要: class Other<T> { public class Inner<U, V> { static Inner() { Console.WriteLine("Other<{0}>.Inner<{1},{2}>", typeof(T).Name, typeof(U).Name, typeof(V).Name); } public static void DummyMethod() { } } } class P... 阅读全文

posted @ 2012-05-01 13:46 xiacy 阅读(303) 评论(0) 推荐(0) 编辑

3.4.1 证明不同 服装类型具有不同的静态字段

摘要: class TypeWithField<T> { public static string field; public static void PrintField() { Console.WriteLine(field + ":" + typeof(T).Name); } } class Program { static void Main(string[] args) { TypeWithField<int>.field = "fi... 阅读全文

posted @ 2012-05-01 13:38 xiacy 阅读(210) 评论(0) 推荐(0) 编辑

3.3.3 用==和 != 进行引用比较

摘要: static bool AreReferencesEqual<T>(T first, T second) where T:class { return first == second; } static void Main(string[] args) { string name = "Jon"; string intro1 = "My name is :" + name; string intro2 = "My name is :" + name; ... 阅读全文

posted @ 2012-05-01 12:42 xiacy 阅读(201) 评论(0) 推荐(0) 编辑

3.3.3 以泛型方式将一个给定的值和默认值比较

摘要: static int ComparaeToDefaults<T>(T value) where T : IComparable<T> { return value.CompareTo(default(T)); } static void Main(string[] args) { Console.WriteLine(ComparaeToDefaults("x")); Console.WriteLine(ComparaeToDefaults("")); ... 阅读全文

posted @ 2012-05-01 12:00 xiacy 阅读(143) 评论(0) 推荐(0) 编辑

3.2.2 在非泛型类型中实现泛型方法

摘要: static List<T> MakeList<T>(T first, T second) { List<T> list = new List<T>(); list.Add(first); list.Add(second); return list; } static void Main(string[] args) { List<string> list = MakeList<string>("Line 1", "Line 2... 阅读全文

posted @ 2012-05-01 11:46 xiacy 阅读(283) 评论(0) 推荐(0) 编辑

3.2.1 泛型方法(List<T>.ConvertAll<TOutput>方法实战)

摘要: static double TakeSqrt(int x) { return Math.Sqrt(x); } static void Main(string[] args) { List<int> integers = new List<int>(); integers.Add(1); integers.Add(2); integers.Add(3); integers.Add(4); ... 阅读全文

posted @ 2012-05-01 11:37 xiacy 阅读(357) 评论(0) 推荐(0) 编辑

3.2.1 泛型例子,泛型字典统计文本中的单词数

摘要: static Dictionary<string, int> CountWords(string text) { Dictionary<string, int> frequencies; frequencies = new Dictionary<string, int>(); string[] words = Regex.Split(text, @"\W+"); foreach (string word in words) { ... 阅读全文

posted @ 2012-05-01 10:53 xiacy 阅读(312) 评论(0) 推荐(0) 编辑

2012年4月25日 #

2.1.1 以各种简单的方式调用委托

摘要: delegate void StringProcessor(string input); class Person { string name; public Person(string name) { this.name = name; } public void Say(string message) { Console.WriteLine("{0} Say: {1}", name, message); } } class Background { ... 阅读全文

posted @ 2012-04-25 23:36 xiacy 阅读(130) 评论(0) 推荐(0) 编辑

1.4.3 LINQ to SQL 对数据库应用查询表达式

摘要: /****** Object: Table [dbo].[Product] Script Date: 04/24/2012 23:58:49 ******/IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Product]') AND type in (N'U'))DROP TABLE [dbo].[Product]GOUSE [C:\USERS\JON\DOCUMENTS\COMPUTING\AUTHORING\CSHARP 2 AND 3\BOOK\CODE\D 阅读全文

posted @ 2012-04-25 00:01 xiacy 阅读(267) 评论(0) 推荐(0) 编辑

2012年4月24日 #

1.4.2 用LINQ to XML 对 XML 文件进行处理

摘要: <?xml version="1.0" encoding="utf-8" ?><Data> <Products> <Product Name="West Side Story" Price="9.99" SupplierID="1"></Product> <Product Name="Assassins" Price="14.99" SupplierID="2">& 阅读全文

posted @ 2012-04-24 23:11 xiacy 阅读(160) 评论(0) 推荐(0) 编辑

1.4.1 LINQ 查询表达式和进程内查询

摘要: 使用查询表达式的前几步:筛选集合List<Product3> products = Product3.GetSampleProduct();var filtered = from Product3 p in products where p.Price > 10 select p;foreach (Product3 product in filtered) Console.WriteLine(product);联接(joining)、筛选(filtering)、排序(ordering)和投影(projectin... 阅读全文

posted @ 2012-04-24 22:49 xiacy 阅读(200) 评论(0) 推荐(0) 编辑

1.2.1 数据的过滤

摘要: C# 1.0 测试循环和打印ArrayList products = Product1.GetSampleProducts();foreach (Product1 product in products){ if (product.Price > 10m) Console.WriteLine(product);}C# 2.0 测试和打印分开进行List<Product2> products = Product2.GetSampleProducts();Predicate<Product2> test = delegate(Product2 p) { return 阅读全文

posted @ 2012-04-24 22:27 xiacy 阅读(252) 评论(0) 推荐(0) 编辑

2012年4月23日 #

1.1.1 从简单的数据类型开始

摘要: /// <summary> /// C# 1.0 中定义的产品类型 /// </summary> public class Product1 { string name; public string Name { get { return name; } } decimal price; public decimal Price { get { return price; } } public Product1(string name, decimal price) { ... 阅读全文

posted @ 2012-04-23 23:36 xiacy 阅读(341) 评论(0) 推荐(0) 编辑

2011年10月24日 #

c#直接对计算机的控制面板等调用

摘要: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Diagnostics; namespace Ex224 { public partial class Form1 : Form { public Form1() ... 阅读全文

posted @ 2011-10-24 10:23 xiacy 阅读(249) 评论(0) 推荐(0) 编辑

2011年10月13日 #

导出excel

摘要: StringBuilder sb = new StringBuilder();//标题 sb.Append("MAC地址,生产商,报备厂商,终端类型,硬件版本,软件版本kernel,软件版本APP,上线IP,开机时间\n");//内容 for (int i = 0; i < itemCount; i++) { sb.Append(((Label)rptList.Items[i].FindControl("lblMac")).Text + ","); ... 阅读全文

posted @ 2011-10-13 14:27 xiacy 阅读(194) 评论(0) 推荐(0) 编辑

2011年10月9日 #

对老赵写的简单性能计数器的修改

摘要: 早上看到老赵写的这个性能计数器,感觉很实用,不过老赵用了很多.C# 3.0 的新语法,还用了 VISTA 和 Server 2008 下特有的Win32 API,对于还在用C#2.0 或者还工作在 XP 或者 Server 2003 下的兄弟们,只能望代码心叹了。应老赵的要求,我修改了他的代码,增加了对低版本C# 和 低版本windows 操作系统的支持。 老赵的原文: 一个简单的性能计数器:CodeTimer 修改说明 1. 采用 接口 取代了原代码中的 Lambda 表达式 2. 采用 GetThreadTimes 这个API 函数替代了原代码中的 QueryThreadCycleTime 阅读全文

posted @ 2011-10-09 15:39 xiacy 阅读(303) 评论(0) 推荐(0) 编辑

反射-利用泛型把枚举绑定到listControl

摘要: using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Reflection;namespace WindowsFormsApplication1{ public partial class Form1 : Form { public Form1() ... 阅读全文

posted @ 2011-10-09 09:56 xiacy 阅读(169) 评论(0) 推荐(0) 编辑

利用反射绑定下拉列表

摘要: using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Reflection;namespace WindowsFormsApplication1{ public partial class Form1 : Form { public Form1() ... 阅读全文

posted @ 2011-10-09 09:44 xiacy 阅读(184) 评论(0) 推荐(0) 编辑

2011年10月8日 #

C#.net反射-代码事例

摘要: using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data;using System.Reflection;namespace 反射{ class Program { static void Main(string[] args) { ReflectionTest test=new ReflectionTest(); test.ClassMemberInfo(); ... 阅读全文

posted @ 2011-10-08 16:37 xiacy 阅读(234) 评论(0) 推荐(0) 编辑

.net中的反射机制的使用

摘要: .NET反射的定义:审查元数据并收集关于它的类型信息的能力。元数据是一种二进制信息,用以对存储在公共语言运行库可移植可执行文件 (PE) 文件或存储在内存中的程序进行描述。将您的代码编译为 PE 文件时,便会将元数据插入到该文件的一部分中。而将代码转换为 Microsoft 中间语言 (MSIL) 并将其插入到该文件的另一部分中。在模块或程序集中定义和引用的每个类型和成员都将在元数据中进行说明。当执行代码时,运行库将元数据加载到内存中,并引用它来发现有关代码的类、成员、继承等信息。元数据以非特定语言的方式描述在代码中定义的每一类型和成员。元数据存储以下信息:程序集的说明:1. 标识(名称、版本 阅读全文

posted @ 2011-10-08 13:47 xiacy 阅读(296) 评论(0) 推荐(0) 编辑

2011年9月30日 #

js添加删除行

摘要: <html xmlns="http://www.w3.org/1999/xhtml"><head> <title></title> <script type="text/javascript"> function insertRow(tbIndex) { var objRow = Binddata.insertRow(tbIndex); var objCel = objRow.insertCell(0); objCel.style.display = "none"; objC 阅读全文

posted @ 2011-09-30 11:13 xiacy 阅读(339) 评论(0) 推荐(0) 编辑

2011年9月8日 #

背完这些基本可以纯键盘操作

摘要: 背完这些基本可以纯键盘操作Ctrl+S 保存Ctrl+W 关闭程序Ctrl+N 新建Ctrl+O 打开Ctrl+Z 撤销Ctrl+F 查找Ctrl+X 剪切Ctrl+C 复制Ctrl+V 粘贴Ctrl+A 全选Ctrl+[ 缩小文字Ctrl+] 放大文字Ctrl+B 粗体Ctrl+I 斜体Ctrl+U 下划线Ctrl+Shift 输入法切换Ctrl+空格 中英文切换Ctrl+回车 QQ号中发送信息Ctrl+Home 光标快速移到文件头Ctrl+End 光标快速移到文件尾Ctrl+Esc 显示开始菜单Ctrl+Shift+< 快速缩小文字Ctrl+Shift+> 快速放大文字Ctrl 阅读全文

posted @ 2011-09-08 08:09 xiacy 阅读(233) 评论(0) 推荐(0) 编辑