krb
krb

Reputation: 16325

Is it possible to implement a C# constructor without using the class name?

When looking at disassembled .NET assemblies I notice that constructors are defined as ".ctor". Is that possible to do in the actual code?

Upvotes: 1

Views: 677

Answers (4)

Henri
Henri

Reputation: 5113

Dont see the point why you want this.

You can use the VS snippet ctor and you get a free constructor.

Just type ctor and press two times the tab-key.

Upvotes: 3

Martin Smith
Martin Smith

Reputation: 453608

RE:

Damn ... it is much easier to use that in code snippets

Is this for Visual Studio code snippets? They already have one called ctor that substitutes the class name. You could look at the definition of that if its default behaviour is not what you want.

<?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>

Upvotes: 3

Michael Petrotta
Michael Petrotta

Reputation: 60942

This is only a syntactic difference between C# and IL. You need to call out the class name when defining the constructor in your C# code. What are you looking to accomplish?

Upvotes: 2

Motti
Motti

Reputation: 114765

Sure, if you write in IL not if you write in C# though.

Upvotes: 5

Related Questions