惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Attack and Defense Labs
Attack and Defense Labs
T
Threatpost
C
Cybersecurity and Infrastructure Security Agency CISA
H
Hackread – Cybersecurity News, Data Breaches, AI and More
I
Intezer
C
Cyber Attacks, Cyber Crime and Cyber Security
The Register - Security
The Register - Security
量子位
Security Latest
Security Latest
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
大猫的无限游戏
大猫的无限游戏
小众软件
小众软件
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
C
CXSECURITY Database RSS Feed - CXSecurity.com
MyScale Blog
MyScale Blog
J
Java Code Geeks
Apple Machine Learning Research
Apple Machine Learning Research
Google DeepMind News
Google DeepMind News
WordPress大学
WordPress大学
Spread Privacy
Spread Privacy
Jina AI
Jina AI
博客园 - 【当耐特】
P
Palo Alto Networks Blog
Last Week in AI
Last Week in AI
SecWiki News
SecWiki News
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
G
GRAHAM CLULEY
宝玉的分享
宝玉的分享
Hacker News - Newest:
Hacker News - Newest: "LLM"
T
The Blog of Author Tim Ferriss
V
Vulnerabilities – Threatpost
有赞技术团队
有赞技术团队
T
Tor Project blog
H
Hacker News: Front Page
A
Arctic Wolf
NISL@THU
NISL@THU
A
About on SuperTechFans
云风的 BLOG
云风的 BLOG
Engineering at Meta
Engineering at Meta
V
V2EX
N
News and Events Feed by Topic
Webroot Blog
Webroot Blog
Know Your Adversary
Know Your Adversary
P
Privacy International News Feed
I
InfoQ
D
Docker
L
LINUX DO - 最新话题
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
U
Unit 42

博客园 - 山峰旺旺

博文阅读密码验证 - 博客园 2019年春节第一天上班 FastDFS搭建文件系统(单机版) 博文阅读密码验证 - 博客园 CentOS7 下docker 部署 Asp.Net Core Linux (CentOS7.0)安装Asp.Net Core项目总结 Linux 下 安装 .Net Core(CentOS 7) Spring框架IOC容器和AOP解析(转) 文本相似度simhash算法 Intellij Idea 2017.3版本MAVEN项目打JAR包 java 敏感词过滤 (DFA算法)(转) intellij idea 修改背景保护色&&修改字体&&快捷键大全(转) C#队列Queue实现一个简单的电商网站秒杀程序 2018年目标和愿景 CDN原理(转,学习用) 面试理论整理 C# unsafe(fixed) 介于 managed code & unmanaged code之间的特性(转) C#中重写(override)和覆盖(new)的区别 (备注:转,留自己用) 2015年总结
C#中的where泛型约束中的new()使用(转)
山峰旺旺 · 2016-06-17 · via 博客园 - 山峰旺旺

在MSDN上面对new()解释说到是where字句的构造函数约束,带有new()约束的任何类型都必须有可访问的无参构造函数,正常来说C#创建的类默认都有一个无参的构造函数,即使你没有写,但是如果你写了一个有参数的构造函数后,那么就没有默认无参的那个了,就需要自己手动写一个。

还是拿前两天的国籍那个举个例子:

    /// <summary>

    /// 国籍的接口

    /// </summary>

    public interface INationality

    {

        string Nationality

        {

            set;

        }

        string GetNationality();

    }

国籍的接口不用改变还是这个,继承此接口的类稍微修改了一下,为每个类增加一个构造方法,改造如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

    /// <summary>

    /// 中国人

    /// </summary>

    public class Chinese : INationality

    {

        public Chinese(string DefaultNationality)

        {

            _Nationality = DefaultNationality;

        }

        private string _Nationality;

        public string Nationality

        {

            set

            {

                _Nationality = value;

            }

        }

        public string GetNationality()

        {

            return string.IsNullOrEmpty(_Nationality) ? "Chinese." : _Nationality;

        }

    }

    /// <summary>

    /// 美国人

    /// </summary>

    public class American : INationality

    {

        public American(string DefaultNationality)

        {

            _Nationality = DefaultNationality;

        }

        private string _Nationality;

        public string Nationality

        {

            set { _Nationality = value; }

        }

        public string GetNationality()

        {

            return string.IsNullOrEmpty(_Nationality) ? "American." : _Nationality;

        }

    }

其实变动也不大,泛型类也没有修改,但是会报错,在创建实例化对象的地方,如下:

1

2

3

4

5

6

7

8

9

10

11

12

    /// <summary>

    ///

    /// </summary>

    /// <typeparam name="T"></typeparam>

    public class PrintNationality<T> where T : INationality, new()//由于此处有new()的约束,所以编译器编译的时候无法通过,那么就将new()去掉,或者为继承INationality的类增加public类型的无参构造函数

    {

        //T item = new T();     在这个地方就不能创建实例化对象了,会提示错误“必须有具有公共无参构造函数的非抽象类型,才能用作泛型类型或方法"xxxx"中的参数T”

        public void Print()

        {

            //Console.WriteLine(string.Format("Nationality:{0}", item.GetNationality()));

        }

    }

要解决上面的问题,就是给继承INationality的接口的每个类都增加一个无参的public型的构造方法,或者,将派生类后面的new()去掉。这样的话,当实例化泛型类的时候就不会有问题了。

上面写那么多也是举一个小例子,其实MSDN说的很明白,就是where字句后面有new()约束的话,T类型必须有公有的无参的构造函数。

又一篇相关文章:

对于new()约束,大家可能有一个误解,以为使用了new约束之后,在创建对象时与非泛型的版本是一致的:
 public class Tester<T> where T:new()    

{        

public Tester()        

{            

  t = new T();//等同于非泛型版本的new? 例如 object o = new object();?        

}

     private T t;

    }
事实上,使用new关键字的作用只是让编译器在泛型实例化之处,检查所绑定的泛型参数T是否具有公共无参构造函数(public 无参构造函数):


 Tester<SomeType> t = new Tester<SomeType>(); //此处编译器会检查SomeType是否具有无参构造函数。若没有则会有compile error。

而Tester<T>类的构造函数中的new代码,实际上等同于下面的代码:
 public class Tester<T> where T:new()    

{        

       public Tester()        

        {             

              t = System.Activator.CreateInstance<T>();        

        }

        private T t;

    }
也就是说,仍然是用反射机制来获取泛型对象的实例的。