1

Suppose there are 3 members in a team and all of them wants to work on a same class. So in this case can we use more than 2 partial class in c#?

5
  • Thats solving a problem by creating more problems. Also partial classes are iffy at best and a major code smell. i suggest using git or some reliable version control Commented Jun 12, 2019 at 4:42
  • 4
    Was it easier for you to post this question than to try it? Honestly it would take 30 seconds. Commented Jun 12, 2019 at 4:43
  • Unrelated to partial classes but still related to the context of your question - there is a tool for real time code collaboration called Visual Studio Live Share. I haven't tried it, but might be worth checking out. Commented Jun 12, 2019 at 5:16
  • 1
    @TheGeneral I use partial classes sometimes when I need to implement multiple interfaces, one for every interface. I feel it is a easy way to keep the methods grouped. Commented Jun 12, 2019 at 5:38
  • @Magnus there are legitimate use cases indeed, however my point (and opinion) was multi-developer access is not one of them, its just asking for problems. Its using the wrong tool for the wrong job (using a langauge feature as a productivity tool, likely to introduce subtle mistakes, broken code, duplication, bloat, and and just completely unnecessary with version control systems. Its seems this has probably been a part of the documentation since version one and entirely fishy (note angst not directed at you) Commented Jun 12, 2019 at 5:46

2 Answers 2

5

Yes, you can create more than 2 Partial classes. (Source Here)

According to Docs:

When working on large projects, spreading a class over separate files enables multiple programmers to work on it at the same time.

Working Example

using System;

public class Program
{
    public static void Main()
    {
        Foo foo = new Foo();
        foo.PrintMessage1();
        foo.PrintMessage2();
        foo.PrintMessage3();
    }
}

public partial class Foo
{
    public void PrintMessage1()
    {
        Console.WriteLine("Foo1");
    }  
}

public partial class Foo
{
    public void PrintMessage2()
    {
        Console.WriteLine("Foo2");
    }
}

public partial class Foo
{
    public void PrintMessage3()
    {
        Console.WriteLine("Foo3");
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

I am surprised the documentation says that at all, its the worst use case in the history of partial classes
IMO partial classes are best used when automatic code generation is involved. As @TheGeneral mentioned, that is the worst use case.
@TheGeneral could you provide a link showcasing the shortcomings and pitfalls of multiple programmers working concurrently with the same class?
@TheodorZoulias Partial classes may contain conflicts at run time.
3

I would not introduce partial classes, to be able to cowork with other developers. A good practice is to use some kind of version control like Git, TortoiseSVN and TFVC. Just to name a few.

Version Control serves multiple purposes:

  • Helping you collaborate. E.g. with merge features.
  • Store your Versions (pretty obvious)
  • Restore old Versions and Snapshots (also called Commits)
  • Keep track of your changes. Review File histories, and see who wrote which code.
  • Backup

Also note, that any Version Control greatly benefits from Merge Tools. These are used to merge multiple File Versions - often submitted by different developers - into one file. It is often refered to as solving merge-conflicts in Version Control.

Jumpstart into Version Control

To give you a little jumpstart, maybe try the following combination:

Use GitExtensions as a graphical interface for the git Version Control. Use Meld to solve merge conflicts.


Summary

To use Version Control in Software development is a major bestpractice, which greatly helps in project management, agility, collaboration and documentation.

To be honest, I've never been working anywhere, where we did not use this nor do I know anyone who didn't. So even though, I'm not aware of the OP's background, I'd still suggest to give it a shot.

Also: Even the Kings of collaboration - developers working together on big opensource projects, work with Version Control. Mostly git.

2 Comments

Not really an answer to the question, but a good explanation why not to use that workflow. Not sure if +1 or not ... eh, here you go ;p
@nilsK thanks a lot. Anyway according to this meta post don't do this is a valid answer :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.