Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 方法名中的new()是什么意思_C#_Syntax_New Operator - Fatal编程技术网

C# 方法名中的new()是什么意思

C# 方法名中的new()是什么意思,c#,syntax,new-operator,C#,Syntax,New Operator,我正在阅读库的代码,我看到了以下语法。我在谷歌上搜索了很多,想找出语法名称,但什么也没找到。任何帮助都将不胜感激 /// <summary> /// Returns a singleton object that is used to manage the creation and /// execution of setup /// </summary> /// <typeparam

我正在阅读库的代码,我看到了以下语法。我在谷歌上搜索了很多,想找出语法名称,但什么也没找到。任何帮助都将不胜感激

        /// <summary>
        /// Returns a singleton object that is used to manage the creation and
        /// execution of setup
        /// </summary>
        /// <typeparam name="TMvxSetupSingleton">The platform specific setup singleton type</typeparam>
        /// <returns>A platform specific setup singleton</returns>
        protected static TMvxSetupSingleton EnsureSingletonAvailable<TMvxSetupSingleton>()
           where TMvxSetupSingleton : MvxSetupSingleton, new()
        {
            // Double null - check before creating the setup singleton object
            if (Instance != null)
                return Instance as TMvxSetupSingleton;
            lock (LockObject)
            {
                if (Instance != null)
                    return Instance as TMvxSetupSingleton;

                // Go ahead and create the setup singleton, and then
                // create the setup instance. 
                // Note that the Instance property is set within the 
                // singleton constructor
                var instance = new TMvxSetupSingleton();
                instance.CreateSetup();
                return Instance as TMvxSetupSingleton;
            }
        }
//
///返回一个单例对象,该对象用于管理创建和
///安装程序的执行
/// 
///特定于平台的设置单例类型
///特定于平台的设置单例
受保护的静态TMvxSetupSingleton EnsureSingletonAvailable()
其中TMvxSetupSingleton:MvxSetupSingleton,new()
{
//在创建setup singleton对象之前进行双重null检查
if(实例!=null)
将实例返回为TMvxSetupSingleton;
锁定(锁定对象)
{
if(实例!=null)
将实例返回为TMvxSetupSingleton;
//继续创建设置单例,然后
//创建安装程序实例。
//请注意,实例属性是在
//单例构造函数
var instance=new TMvxSetupSingleton();
CreateSetup();
将实例返回为TMvxSetupSingleton;
}
}

请注意,new(){。这是什么意思?

来自Microsoft文档

where子句还可能包含构造函数约束new()。该约束使使用new运算符创建类型参数的实例成为可能。new()约束使编译器知道提供的任何类型参数都必须具有可访问的无参数构造函数。例如:

public class MyGenericClass<T> where T : IComparable<T>, new()
{
    // The following line is not possible without new() constraint:
    T item = new T();
}
公共类MyGenericClass,其中T:IComparable,new()
{
//如果没有new()约束,则无法执行以下行:
T项=新的T();
}
new()约束出现在where子句的最后一个。new()约束不能与结构或非托管约束组合。满足这些约束的所有类型都必须具有可访问的无参数构造函数,这使得new()约束多余


来自Microsoft文档

where子句还可能包含构造函数约束new()。该约束使使用new运算符创建类型参数的实例成为可能。new()约束使编译器知道提供的任何类型参数都必须具有可访问的无参数构造函数。例如:

public class MyGenericClass<T> where T : IComparable<T>, new()
{
    // The following line is not possible without new() constraint:
    T item = new T();
}
公共类MyGenericClass,其中T:IComparable,new()
{
//如果没有new()约束,则无法执行以下行:
T项=新的T();
}
new()约束出现在where子句的最后一个。new()约束不能与结构或非托管约束组合。满足这些约束的所有类型都必须具有可访问的无参数构造函数,这使得new()约束多余


根据MSDN:

new
约束指定泛型类声明中的类型参数必须具有公共无参数构造函数。若要使用
new
约束,类型不能是抽象的

当创建泛型类时,将
new
约束应用于类型参数 创建该类型的新实例,如下例所示:


根据MSDN:

new
约束指定泛型类声明中的类型参数必须具有公共无参数构造函数。若要使用
new
约束,类型不能是抽象的

当创建泛型类时,将
new
约束应用于类型参数 创建该类型的新实例,如下例所示:

public class ItemFactory2<T>
    where T : IComparable, new()
{  }