夜的第七章

ASP.NET、C#、.NET开发资源、Javascript等等为主的开发备注收藏注意警告等的一个个人博客。

« 关于谷歌关闭.CN,关于美国的强权政治,关于中国屏幕养眼的苹果绿 »

备忘C#新特性:??操作符与参数中的this,及protected internal,where new()

int? len = null;

int max = len ?? 10;

用于在一个可空类型的元素上做三元运算时的快捷方式,如果不为null则取它的值,如果为null,则取后面的值(这里是10)。

public static class Class1
{
        public static int Test(this int aaa)
        {
            return 0;
        }
}

this操作符,标识该方法是作为后跟类型(这里是int)的扩展方法,即在int类型上增加Test()方法。

 

附protected internal访问修饰符,这个比较少用:

protected限定的是只有在继承的子类中才可以访问,可以跨程序集   
internal限定的是只有在同一个程序集中才可以访问,可以跨类 
protected internal 的访问范围是:本程序集、继承的子类。

很长时间不用,就会忘掉的。。。。

2010-05-21更新:

public interface IEntityProvider<Entity, EntityKey>
  where Entity : IEntityId<EntityKey>, new()
  where EntityKey : IEntityKey, new()


where T : new() 表示T这个类型需要拥有一个无参的构造方法。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

Powered By Z-Blog 1.8 Arwen Build 81206



©2007 - 2010 www.leadnt.com 保留一些权利吧