终于有空停下来看一下这些看起来还比较陌生的东西。
学习之前也按部就班的看了下Linq to sql的准备知识,其实也是c#里边一些新加的特性。
学习中的体会收录于此,希望对像我这样对Linq to sql的初学者有所裨益。
 1. 扩展方法。      
  • 被扩展的类一定要包含对扩展方法类的引用。 
          
  • 被扩展的类的内部方法优先于扩展方法。
          
  • 被扩展的方法如果有重载,也是以被扩展类优先。
Example:

Extension Method
  调用输出:ExtensionMethodDemo.TestExtensionMethod();
  结果:
Here is A.MethodA();
Here is ExtensionMethod.MethodA(this IExtension iInstance,int i);
Here is ExtensionMethod.MethodB(this IExtension iInstance,string s);
Here is B.MethodA();
Here is B.MethodB(int i);
Here is ExtensionMethod.MethodB(this IExtension iInstance,string s);
Here is C.MethodA();
Here is C.MethodA(object o);
Here is C.MethodA(object o);

2、对象初始化器:
使用对象初始化器,如果有重载的构造函数,则必须显式重写默认构造函数。
namespace LinqPreparation
{
    
/// <summary>
    
/// Auto Property
    
/// </summary>
    public class Person
    {
        
public string name { getset; }
        
public int age { getset; }

        
public Person()
        {
        }

        
public Person(string name, int age)
        {
            
this.name = name;
            
this.age = age;
        }

        
public override string ToString()
        {
            
return string.Format("Name:{0},Age:{1}",name,age);
        }
    }
}
调用:
        public class ObjectInialLize
        {
            
public static void ShowObjectInitialize()
            {
                
//Must have default construction method
                Person p = new Person() { name="Arisity", age=5 };
                Console.WriteLine(
@"Person p = new Person() { name=""Arisity"", age=""5"" };");
                Console.WriteLine(p.ToString());
            }
        }

以上只是个人之拙见,欢迎批评指正。
未完待续……
posted on 2009-09-04 12:06  沉默的心  阅读(853)  评论(0编辑  收藏  举报