随笔分类 - C#
摘要:1. What is delegate in C#?A delegate is an object that knows how to call a method.A delegate type defines the kind of method that delegate instances c...
阅读全文
摘要:using System;public static class GreaterTest{ public static of( left, right) {) .MakeGenericType(type).IsAssignableFrom(intf) || ...
阅读全文
摘要:我们在写一些Model的时候,经常会重写ToString,为了在控制台中进行打印或者更好的单元测试。但是,如果Model的字段非常多的时候,如此简单的重复劳动经常会变成一件令人头痛的事情,因为大家都不想重复劳动,或者这种事情应该交给初级程序员或者毕业生去做。看如下:public class Cust...
阅读全文
摘要:var query = from ll in proListRequestList group ll by new {ll.pro_id} into g select new ProListRequest { pro_id = g.Key.pro_id, number = g.Sum(x => x....
阅读全文
摘要:using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ReflectionTest{ public class Employee { public string Name { get; set; } public int Age { get; set; } }}using System;using System.Collections.Generic;using System.Linq;using System.Text...
阅读全文
摘要:Thread.Join()在MSDN中的解释很模糊:Blocks the calling thread until a thread terminates有两个主要问题:1.什么是the calling thread? 2.什么是a thread? 首先来看一下有关的概念: 我们执行一个.exe文件实际上就是开启了一个进程,同时开启了至少一个线程,但是真正干活的是线程,就好比一个Team有好几个人,但是真正干活的是人不是Team. 具体到代码来说,以Console Application为例:程序Test.exe从Main函数开始运行,实际上是有一个线程在执行Main函数...
阅读全文
摘要:What’s New in C# 3.0 Language Integrated Query(LINQ) - LINQ enables queries to be written in C# program to query both local collections (such as lists or XML documents) or remote data sources (such as a database) Implicitly typed local variables - let you omit the variable type in a declaration stat
阅读全文
摘要:1. 创建一个单例模式的自定义线程池类public class CustomThreadPool{ //#region configurable items - for demo let's have these as constants private const int MAX = 8; // maximum no of threads in pool private const int MIN = 3; // minimum no of threads in pool private const int MIN_WAIT = 10; // milliseconds...
阅读全文
摘要:经常我们会发现,当我们把一个对象列表赋值给另一个对象列表之后,一个改变了,另一个也跟着改变了,但是这往往不是我们想看到的那么,怎么办呢?办法只有一个,那就是让你的对象实现IClonable接口对象代码:publicclassEmployee:ICloneable{publicintEmployeeID{get;set;}publicstringLastName{get;set;}publicstringFirstName{get;set;}publicstringTitle{get;set;}publicstringCity{get;set;}publicstringRegion{get;s.
阅读全文
摘要:1. introducing the Func<…> delegate types Here are the signaturesof all the Func delegate types:public delegate TResult Func<TResult>()public delegate TResult Func<T,TResult>(T arg)public delegate TResult Func<T1,T2,TResult>(T1 arg1, T2 arg2)public delegate TResult Func<T1
阅读全文
摘要:1. 装箱(boxing) 用于把一个值类型转换为引用类型inti=20;objecto=i;//隐式转换2. 拆箱(unboxing) 将以前装箱的值类型转换回引用类型inti=20;objecto=i;intj=(int)o;
阅读全文
摘要:有人经常问String和StringBuilder有什么区别,啥时候用什么呢?其实,如果是简单的字符串应用的话,一般都用string。下面我们来举个例子:[代码]以上属于比较丑陋的代码了,像这种情况,至少,我们应该利用一下C#中的字符串换行符号@和string.Format吧。可以这样写:[代码]但是以上写法还不是最好的,这个时候我们想到了StringBuilder。因为在比较重量级的字符串拼接的...
阅读全文
摘要:我们在处理数据库程序的时候,经常会碰到这样2个东东DBNull.Value 和 Null,那么它们到底有什么区别呢?区别如下:DBNull是C#中的一个处理数据库中Null值的一个类,它有一个属性是 Value.如果你检索出来的数据,有记录,但是某个字段的值是Null,那么它就等同于DBNull.Value.而Null代表什么都没有,如果你一行记录都没有检索出来,那么这个时候你变量的值就是Null...
阅读全文
摘要:数组列表类似于数组,但它是可以动态增加的,类全名为:System.Collections.ArrayList。ArrayList的默认创建容量为16,你也可以指定大小,因此,下面两种方法均可以:ArrayList a1 = new ArrayList();ArrayList a2= new ArrayList(20);为了便于理解,我们举个简单的例子,如下:[代码]在上面的例子中,我们既可以通过A...
阅读全文
摘要:我们知道,数组是System.Array类的一个实例。但是,数组有很大的缺点,就是需要指定大小,也不能添加,插入,删除元素。因此,在.NET中引入了集合的概念,所有的集合类都存放在System.Collections命名空间下。所有的集合必须实现 System.Collections.IEnumerable接口,该接口的原型如下:[代码]System.Collections.IEnumerator...
阅读全文