0

I have a partial class with no constructor code. I want to add constructor code but is it possible to add this constructor in another class which is a part of this partial class.

2
  • It is a partial class auto-generated using T4 template but there is no constructor code. I want to add a constructor to this class but I want this new class to be located in another folder. Commented Jul 5, 2012 at 7:11
  • 1
    @RPK, add the new cs file in the folder, name the class as public partial class <yourclass> and make sure you specify the same namespace as in generated class Commented Jul 5, 2012 at 7:23

4 Answers 4

3

add this constructor in another class

No, you can't.

But in the same partial class or may be on a different file, yes

public partial class Test
{
}

public partial class Test
{
    public Test()
    {
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

So this means another partial class with the same name.
+1. @RPK, no it means the same class implemented in separate files by using partial to split implemetation between files.
partial class is a single class, split over multiple source files or same file like above, see msdn.microsoft.com/en-us/library/wa80x488(v=vs.80).aspx
1

You can add a constructor to a partial class, if your class is partial to that class!

Here's an example of what you can do:

public partial class Test
{
    public string Name { get; set; }

    public Test(string name, int age)
    {
        Name = name;
        Age = age;
    }
}

public partial class Test
{
    public int Age { get; set; }

    public Test(string name)
    {
        Name = name;
    }
}

Essentially this would be the same as doing:

public class Test
{
    public int Age { get; set; }
    public string Name { get; set; }

    public Test(string name)
    {
        Name = name;
    }

    public Test(string name, int age)
    {
        Name = name;
        Age = age;
    }
}

1 Comment

I have a problem. The other partial class (with Constructor) is also with the same name but I am unable to access the properties of old partial class. While creating instance, it is red-underlined.
1

It is not possible to add constructor for a class in another class.

What is possible it to have constructor for a class that consist from several files wiht partial class in any of the files for this partial class.

Comments

1

If i understand the question correctly, the answer is that you can add the constructor in any side of the partial class you wish. But you cant add it in a nested class that exists in your partial class, as that is a totally different class. Examples are provided on the official msdn site that make it clear.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.