在 C# 中,new 关键字可用作运算符、修饰符或约束。
1)new 运算符:用于创建对象和调用构造函数。
2)new 修饰符:在用作修饰符时,new 关键字可以显式隐藏从基类继承的成员。
3)new 约束:用于在泛型声明中约束可能用作类型参数的参数的类型。 
 public class Program: BaseClass
{
new public class Test//2、new修饰符 显式隐藏从基类继承的成员
{
public int x = ;
public int y = ;
public int z = ;
} static void Main(string[] args)
{
var c1 = new Test();//1、new操作符 创建对象和调用构造函数
var c2 = new BaseClass.Test();
Console.WriteLine(c1.x);//
Console.WriteLine(c2.y);//
Console.ReadKey();
}
} public class BaseClass
{
public class Test
{
public int x = ;
public int y = ;
}
}

new约束指定泛型类声明中的任何类型参数都必须具有公共的无参数构造函数(http://www.cnblogs.com/kingdom_0/articles/2023499.html

 using System;
using System.Collections.Generic; namespace ConsoleApplication2
{
public class Employee
{
private string name;
private int id; public Employee()
{
name = "Temp";
id = ;
} public Employee(string s, int i)
{
name = s;
id = i;
} public string Name
{
get { return name; }
set { name = value; }
} public int ID
{
get { return id; }
set { id = value; }
}
} class ItemFactory<T> where T : new()
{
public T GetNewItem()
{
return new T();
}
} public class Test
{
public static void Main()
{
ItemFactory<Employee> EmployeeFactory = new ItemFactory<Employee>();
////此处编译器会检查Employee是否具有公有的无参构造函数。
//若没有则会有The Employee must have a public parameterless constructor 错误。
Console.WriteLine("{0}'ID is {1}.", EmployeeFactory.GetNewItem().Name, EmployeeFactory.GetNewItem().ID);
}
}
}

C#中new的三种用法的更多相关文章

  1. java中 this 的三种用法

    Java中this的三种用法 调用属性 (1)this可以调用本类中的任何成员变量 调用方法(可省略) (2)this调用本类中的成员方法(在main方法里面没有办法通过this调用) 调用构造方法 ...

  2. c++中new的三种用法详细解析

    转载至: http://www.jb51.net/article/41524.htm 以下的是对c++中new的三种使用方法进行了详细的分析介绍,需要的朋友可以过来参考下,希望对大家有所帮助. 一. ...

  3. mybatis foreach中collection的三种用法

    原文:https://www.cnblogs.com/xiemingjun/p/9800999.html foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合. foreach ...

  4. foreach中collection的三种用法

    转载:http://blog.sina.com.cn/s/blog_b0d90e8c0102v1q1.html 传参参考:http://www.cnblogs.com/ruiati/p/6410339 ...

  5. React中ref的三种用法 可以用来获取表单中的值 这一种类似document.getXXId的方式

    import React, { Component } from "react" export default class MyInput extends Component { ...

  6. JAVA中this的三种用法的详解

    this关键字必须放在非静态方法里面 this关键字代表自身,在程序中主要的使用用途有以下几个方面: ? 使用this关键字引用成员变量 ? 使用this关键字在自身构造方法内部引用其它构造方法 ? ...

  7. python中else的三种用法

    与if搭配 要么--不然-- num = input("输入一个数字") if(num % 2 == 0): print("偶数") else: print(& ...

  8. java中 this 关键字的三种用法

    Java中this的三种用法 调用属性 (1)this可以调用本类中的任何成员变量 调用方法(可省略) (2)this调用本类中的成员方法(在main方法里面没有办法通过this调用) 调用构造方法 ...

  9. 子查询。ANY三种用法。ALL两种用法。HAVING中使用子查询。SELECT中使用子查询。

    子查询存在的意义是解决多表查询带来的性能问题. 子查询返回单行多列: ANY三种用法: ALL两种用法: HAVING中的子查询返回单行单列: SELECT中使用子查询:(了解就好,避免使用这种方法! ...

随机推荐

  1. Redis配置主从复制

    Redis配置主从复制 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.操作环境介绍 1>.操作系统环境 [root@node101.yinzhengjie.org.cn ...

  2. Netty 源码分析

    https://segmentfault.com/a/1190000007282628 netty社区-简书闪电侠 :https://netty.io/wiki/related-articles.ht ...

  3. 2018-2019-2 《Java程序设计》第3周学习总结

    20175319 2018-2019-2 <Java程序设计>第3周学习总结 教材学习内容总结 第三周通过课本与蓝墨云上的视频学习了<Java2实用教程>第四章类与对象 成功激 ...

  4. Mongodb注入

    0x01 Brief Description 作为nosql(not only sql)数据库的一种,mongodb很强大,很多企业也在用到.相对于sql数据库,nosql数据库有以下优点:简单便捷. ...

  5. ASP.NET Core学习之五 EntityFrameworkCore

    目的:运用EntityFrameworkCore ,使用codefirst开发 一.创建web项目 创建一个不进行身份验证的   ASP.NET Core Web Application (.NET ...

  6. PYTHON-进程 子进程

    并发编程 学习目标: 见35复习 1.操作系统 什么是操作系统 操作系统的两大作用: 批处理操作系统: 操作系统发展史: 多道技术***** 产生背景: 两大核心点: 应用软件的优化的核心:***** ...

  7. java入门写的第一个代码《HelloWorld》

    public class HelloWorld {public static void main(String[] args){System.out.println("HelloWorld! ...

  8. 关于 layer.mask = label.layer 出现空白情况

    源代码如下: self.numLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width/3, ...

  9. unity读取txt,并且生成vector3的数组

    读取txt的脚本: public void ReadFileTX(){ var str = File.ReadAllText(Application.streamingAssetsPath + &qu ...

  10. pandas 中处理数据的函数和方法