Questions tagged [partial-classes]

With this keyword classes can be split into multiple definitions, but it compiles into one class.

With this keyword, classes can be split into multiple (mostly two) definitions, but the code compiles into one run-time class.

References

441 questions
173
votes
7 answers

Partial classes in separate dlls

Is it possible to have two parts (same namespace, same class name) to a partial class in separate DLLs?
Brad
  • 15,361
  • 6
  • 36
  • 57
141
votes
9 answers

Is it possible to have two partial classes in different assemblies represent the same class?

I have a class called 'Article' in a project called 'MyProject.Data', which acts as the data layer for my web application. I have a separate project called 'MyProject.Admin', which is a web-based admin system for viewing/editing the data, and was…
Jonathan
  • 32,202
  • 38
  • 137
  • 208
113
votes
2 answers

Naming Conventions For Partial Class Files

I'm generating the bulk of my ASP.NET MVC scaffolding code. All generated files are partial classes which use standard naming conventions. For example, my employee controller file is named EmployeeController.cs. If I wish to extend the…
Ben Griswold
  • 17,793
  • 14
  • 58
  • 60
95
votes
5 answers

Can I define properties in partial classes, then mark them with attributes in another partial class?

Is there a way I can have a generated code file like so: public partial class A { public string a { get; set; } } and then in another file: public partial class A { [Attribute("etc")] public string a { get; set; } } So that I can…
Chris McCall
  • 10,317
  • 8
  • 49
  • 80
57
votes
8 answers

Should you use a partial class across projects?

I have a class library with all my database logic. My DAL/BLL. I have a few web projects which will use the same database and classes, so I thought it was a good idea to abstract the data layer into its own project. However, when it comes to…
Armstrongest
  • 15,181
  • 13
  • 67
  • 106
53
votes
13 answers

Override Default Constructor of Partial Class with Another Partial Class

I don't think this is possible, but if is then I need it :) I have a auto-generated proxy file from the wsdl.exe command line tool by Visual Studio 2008. The proxy output is partial classes. I want to override the default constructor that is…
Elijah Manor
  • 17,923
  • 17
  • 72
  • 79
51
votes
6 answers

Is it possible to do static partial classes?

I want to take a class I have and split it up into several little classes so it becomes easier to maintain and read. But this class that I try to split using partial is a static class. I saw in an example on Stackoverflow that this was possible to…
OmniOwl
  • 5,477
  • 17
  • 67
  • 116
50
votes
9 answers

Is there any Python equivalent to partial classes?

Using "new" style classes (I'm in python 3.2) is there a way to split a class over multiple files? I've got a large class (which really should be a single class from an object-oriented design perspective, considering coupling, etc, but it'd be nice…
Matthew Lund
  • 3,742
  • 8
  • 31
  • 41
45
votes
9 answers

Partial Class vs Extension Method

I dont have much experience of using these 2 ways to extend a class or create extension methods against a class. By looking others work, I have a question here. I saw people using a partial class to extend an entity class in a project. Meanwhile, in…
ValidfroM
  • 2,626
  • 3
  • 30
  • 41
44
votes
3 answers

How to extend DbContext with partial class and partial OnModelCreating method in EntityFramework Core

I'm using EF Core and DatabaseFirst approach. My dbContext is created automatically by Scaffold-DbContext command. I need to add some new DbSets into a dbContext and add into OnModelCreating method some additional code but after each scaffolding…
42
votes
10 answers

Best Practices: When not/to use partial classes

I have been using the partial class modifier for some time in order to put helper classes in their own file. Today we got a new guy and he said that the last team he worked with didn't allow partial classes for this because modifying a helper class…
Wayne Bloss
  • 5,370
  • 7
  • 50
  • 81
40
votes
3 answers

Partial Class Constructors

Is there a way to have a partial class' constructor call another method that my or may not be defined? Basically my partial class constructor is defined: public partial class Test { public Test() { //do stuff } } I would…
Omar
  • 39,496
  • 45
  • 145
  • 213
36
votes
2 answers

If a partial class inherits from a class then all other partial classes with the same name should also inherit the same base class?

I have a class in Model in my MVC project like this. public partial class Manager : Employee { public string Name {get;set;} public int Age {get;set;} } And this class I have in App_Code folder in the same project. Now I want to know…
Jitender Kumar
  • 2,439
  • 4
  • 29
  • 43
34
votes
5 answers

A way to implement partial classes in java

I have an interface that I want to implement in separate classes after doing a quick google search apparently, Java doesn't have partial classes. Is there a way that I can do this or am I stuck throwing all of my code into one class? Basically, I…
SoftwareSavant
  • 9,467
  • 27
  • 121
  • 195
24
votes
8 answers

C#: How to set default value for a property in a partial class?

I'm very new to C# so please bear with me... I'm implementing a partial class, and would like to add two properties like so: public partial class SomeModel { public bool IsSomething { get; set; } public List SomeList { get; set; } …
Roee Adler
  • 33,434
  • 32
  • 105
  • 133
1
2 3
29 30