是否可以在不使用类名的情况下实现C#构造函数?

时间:2010-04-11 20:29:48

标签: c# constructor

在查看反汇编的.NET程序集时,我注意到构造函数被定义为“ .ctor ”。这可以在实际代码中做到吗?

4 个答案:

答案 0 :(得分:5)

当然,如果你用IL写的话,如果你用C#编写,那就不是了。

答案 1 :(得分:3)

不要明白你为什么要这样做。

您可以使用VS片段ctor,并获得免费的构造函数。

只需键入ctor并按两次tab键即可。

答案 2 :(得分:3)

RE

  

该死的......它更容易使用   在代码片段中

这适用于Visual Studio代码段吗?他们已经有一个名为ctor的替代类名。如果它的默认行为不是你想要的,你可以看一下它的定义。

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>ctor</Title>
            <Shortcut>ctor</Shortcut>
            <Description>Code snippet for constructor</Description>
            <Author>Microsoft Corporation</Author>
            <SnippetTypes>
                <SnippetType>Expansion</SnippetType>
            </SnippetTypes>
        </Header>
        <Snippet>
            <Declarations>
                <Literal Editable="false">
                    <ID>classname</ID>
                    <ToolTip>Class name</ToolTip>
                    <Function>ClassName()</Function>
                    <Default>ClassNamePlaceholder</Default>
                </Literal>
            </Declarations>
            <Code Language="csharp"><![CDATA[public $classname$ ()
    {
        $end$
    }]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>

答案 3 :(得分:2)

这只是C#和IL之间的语法差异。在C#代码中定义构造函数时,需要调用类名。你想要完成什么?

相关问题