随笔分类 - C#
摘要:IList 和 ICollection最重要的一些类型List: Encapsulates[T], like array, but also provide with adding/removing elemntsReadOnlyCollection: Read-only wrapper for l...
阅读全文
摘要:Core Generic interface IEnumerable:you can interate my elemnts, no need to know the count, can not modify my contents (Array在这里)var myString = new s...
阅读全文
摘要:Collection定义Collection是个关于一些变量的集合,按功能可分为Lists,Dictionaries,Sets三个大类。Lists:是一个有序的集合,支持index look up。ArrayListDictionaries:同TKey store 一些TValue集合,没有特定or...
阅读全文
摘要:Original articleBuilt-in arraysJavascript Arrays(Javascript only)ArrayListsHashtablesGeneric ListsGeneric Dictionaries2D ArraysAll types of collections share a few common features:You can fill them with objects, and read back the values that you put in.You can ‘iterate’ through them, which means you
阅读全文
摘要:OriginalArticle程序下载Often you want to be able to enumerate through a collection of objects using theforeachstatement in C#:Using foreach in C#foreach (Student student in myClass) Console.WriteLine(student);To be able to pull that off, myClass must implementIEnumerable:IEnumerable DefinedExposes th...
阅读全文
摘要:Strong and Loose Typing:强弱比较// C#var customer = new Customer(); //var is compiler inferred//Javascriptvar customer = new Customer(); //var is variable declarationC#:var变量由编译器决定合适的类型,此例为Customer类型JavaScript:var代表变量的意思,没有类型的强制Compiler匹配最接近Type//C#var x = 0; // compiler暗示 to be intbool isInt = x is int
阅读全文
摘要:prepare Employee Structurenamespace CollectIt{ public class Employee { public string Name { get; set; } public int DepartmentId { get; set; } } }Array: T[]定义fix size, must define how many instance at beginning, no add methordEmployee[] employees = new Employee[]{ ...
阅读全文
摘要:命名文件名和Class要一致(CamelCase)类公共和保护类型Property(CamelCase)类的公共和保护类型Fields(CamelCase)* 先采用.Net的命名方法,如果出现问题以后改成小写。Methord和Function(CamelCase)私有字段或者Property(_camelCase)parameter(camelCase)定义变量的位置最好在使用前,离得越近越好Type Var var x = 0; //x is an int var x = 0f; //x is a float var x = new Dictionary(); // x ...
阅读全文
摘要:特性(attribute)是被指定给某一声明的一则附加的声明性信息。 元数据,就是C#中封装的一些类,无法修改.类成员的特性被称为元数据中的注释. 1、什么是特性 1)属性与特性的区别属性(Property):属性是面向对象思想里所说的封装在类里面的数据字段,Get,Set方法。特性(Attribute):官方解释:特性是给指定的某一声明的一则附加 的声明性信息。 允许类似关键字的描述声明。它对程序中的元素进行标注,如类型、字段、方法、属性等。 从.net角度看,特性是一种 类,这些类继承于System.Attribute类,用于对类、属性、方法、事件等进行描述,主要用在反射中。但从面向...
阅读全文
摘要://十进制转二进制 Console.WriteLine(Convert.ToString(69, 2)); //十进制转八进制 Console.WriteLine(Convert.ToString(69, 8)); //十进制转十六进制 Console.WriteLine(Convert.ToString(69, 16)); //二进制转十进制 Console.WriteLine(Convert.ToInt32(”100111101″, 2)); //八进制转十进制 Console.WriteLine(Convert.ToInt32(”76″, 8)); //十六进制转十进制 Console.
阅读全文
摘要:封装field一般为private,定义的时候可以不赋值。不赋值的时候一般被构造函数初始化赋值,其值用来保存类实例的数据,可以被内部方法使用作为计算的数据来源。当需要继承类继承本类的时候,field要改为protected类型,这样继承类时子类可以对基类的field有完全访问权。property一般为public,是向外暴露的窗口,属性可以为其他外部类使用。方法可以是public的,用来向外暴露,外部可以直接调用。继承如果子类继承于父类,第一:子类拥有父类非private的属性和功能;第二:子类具有自己的属性和功能,即子类可以扩展父类没有的属性和功能;第三:子类还可以以自己的方式实现父类的功能
阅读全文
摘要:- System.IO.FileInfo- System.IO.DirectoryInfo MSDN- System.IO.Directory- System.IO.FileView Code using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; namespace ConsoleApplication3 { class Program { stat...
阅读全文
摘要:Field initializers赋值可以直接给值,也可以是对象。建议不直接给filed initialzer赋值,当继承的时候会出现顺序的怪异问题:解决方法:在constructor里给filed赋值注意如果是继承类,自己ctor之前先要ctor基类。叫public Person()的时候会先跳到上面的基类public Base()先。using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ConsoleA
阅读全文
摘要:Nested typesClass和Structure里可以nest任意多的类型(包括class),但是Enum里不可以。class{ enum ParserState { }}Class Person{ Class Room{} enum GunType{}}View Code Access modifiersInternal:只包在本assemble(ClassLibrary编译后是dll,ConsoleApplication16编译后是exe文件,这是两个独立的Assembly)里使用。 其他Assembly不可见。上面ClassLibrary1是一个cla...
阅读全文
摘要:Binary File Operationhttp://blog.sina.com.cn/s/blog_6f7e825501015ogy.htmlhttp://www.mzwu.com/article.asp?id=2364http://blog.csdn.net/longge7685/article/details/4620893StreamWriter & StreamReader最简单的实例:using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Te
阅读全文
摘要:http://broadcast.oreilly.com/2010/09/understanding-c-equality-iequa.html
阅读全文
摘要:赋值1:View Code 1 using System; 2 3 class ComplexNumber 4 { 5 public double Real { get; set; } 6 public double Imaginary { get; set; } 7 8 public override string ToString() 9 {10 return String.Format("{0}{1}{2}i", 11 Real, 12 Imaginary >= 0...
阅读全文
摘要:时刻提醒自己是否这个类或者方法符合SRP,我们需不需要把其分成小份的?single responsibility principle states that every class should have a single responsibility, and that responsibility should be entirely encapsulated by the class. All its services should be narrowly aligned with that responsibility.View Code 1 using System; 2 usin.
阅读全文
摘要:is: return true or falseView Code 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace ConsoleApplication6 7 { 8 abstract class Purchasedable 9 {10 11 }12 abstract class Moveable : Purchasedable13 {14 15 }16 abstrac...
阅读全文