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#?
-
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 controlTheGeneral– TheGeneral2019-06-12 04:42:08 +00:00Commented Jun 12, 2019 at 4:42
-
4Was it easier for you to post this question than to try it? Honestly it would take 30 seconds.John Wu– John Wu2019-06-12 04:43:16 +00:00Commented 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.user6572277– user65722772019-06-12 05:16:15 +00:00Commented 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.Magnus– Magnus2019-06-12 05:38:29 +00:00Commented 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)TheGeneral– TheGeneral2019-06-12 05:46:36 +00:00Commented Jun 12, 2019 at 5:46
2 Answers
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");
}
}
5 Comments
Partial classes may contain conflicts at run time.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.