首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >方法名称中的new()表示C#语法

方法名称中的new()表示C#语法
EN

Stack Overflow用户
提问于 2020-01-08 13:19:33
回答 2查看 961关注 0票数 0

我当时正在阅读一个库代码,我看到了下面的语法。我在Google上搜索了很多“安静”来找出语法名,但是我什么也没找到。任何帮助都将不胜感激。

代码语言:javascript
运行
复制
        /// <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;
            }
        }

请注意,new () {。这意味着什么?

EN

Stack Overflow用户

发布于 2020-01-08 13:24:09

根据MSDN:

new约束指定泛型类声明中的类型参数必须具有公共的无参数构造函数。若要使用new约束,类型不能是抽象的。 当泛型类创建类型的新实例时,将new约束应用于类型参数,如下面的示例所示:

代码语言:javascript
运行
复制
class ItemFactory<T> where T : new()
{
    public T GetNewItem()
    {
        return new T();
    }
}

当您将new()约束与其他约束一起使用时,必须最后指定它:

代码语言:javascript
运行
复制
public class ItemFactory2<T>
    where T : IComparable, new()
{  }

这里有更多的信息

票数 2
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59646770

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档