1

i have a question, why there is a partial class and a virtual method in c#?

i mean what is the difference when i have a normal or abstract class and then inherit from it or make it partial?

the other question is why i need virtual methods? when i inherit from a class i also can override the method if i want.

gurehbgui
  • 14,236
  • 32
  • 106
  • 178
  • 3
    These are two different questions. – Anthony Pegram Oct 26 '11 at 13:56
  • 2
    "When I inherit a class I can override the method if I want." *Only* if the method is marked as `virtual`. Otherwise you can't. – dlev Oct 26 '11 at 13:56
  • 1
    Two different, and, most possibly, duplicated. – Centro Oct 26 '11 at 13:57
  • There must be canonical answers for this somewhere. partial means 'not completely defined in this file', e.g. you can have some part of your class in a separate machine-generated file (such as your windows forms control definitions) and a separate file with code you've added yourself. Yes, you can override the method in a derived class but then if you call the same method on that class through a pointer through a reference declared as a parent type then it'll call the parent method not your override method - unless you make the parent method virtual. – Rup Oct 26 '11 at 13:57

6 Answers6

8

Partial classes: they're mostly there to allow machine-generated code to be combined with manually-generated code at compile time, to avoid abusing inheritance etc. It can be used for other reasons, but that's the main purpose of them. No more source files with "don't edit this bit - it belongs to the designer" etc.

Virtual methods: no, you can't override a member if it's not declared virtual (or abstract) in the base class. You can shadow it with new, but that's not the same as overriding it - the new method won't be called polymorphically.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
5

Partial classes and methods allow you to spread code across different files. This is useful when you use code generation - the generated code is in one file that can get overwritten without a problem, the rest of the code can be changed safely.

As for virtual - in C#, if you want to override a method, you must use virtual (or abstract) in the base class and override in the overriding class. You can't simply override without them.

If you don't use virtual/abstract and override you are in danger of hiding/shadowing a method, which may or may not be what you want. Using virtual/abstract and override make things explicit and predictable.

Community
  • 1
  • 1
Oded
  • 489,969
  • 99
  • 883
  • 1,009
1
  1. Partial classes lets you split a class definition across multiple files, which is practical in some scenarios.
  2. No; you can only override a method if it is marked as virtual in the base class. If the base class' method is not virtual and you create a method with the same name in the subclass, you have created an entirely new, unrelated method.
Aasmund Eldhuset
  • 37,289
  • 4
  • 68
  • 81
1

Partial classes allow you to split up a class definition across several files.

A virtual method may be overridden using the keyword override. If the method is not defined as virtual you can only hide it in a child class using the new keyword.

I guess you can read all this up in any C# 101 material. Just take a look at the MSDN library.

Christoph Grimmer
  • 4,210
  • 4
  • 40
  • 64
0

A partial class let's you define the same class in multiple declaration which are mostly in different files also. Like:

public partial class MyClass{
   public void MethodOne(){}
}

public partial class MyClass{
   public void MethodTwo(){}
}

A virtual method can be used if a subclass of the baseclass has the ability to override the method. Use abstract if your subclass MUST override the method. So,

public class MyBaseClass{
  public virtual void MethodOne(){} // CAN be overridden...

  public abstract void MethodTwo(){} // MUST be overridden.
}
Bas Slagter
  • 9,831
  • 7
  • 47
  • 78
0

Partial classes are useful to divide a single class in multiple files in the same project. This is very useful for tools that generates code for you, like windows form or WPF. Partial classes allow you, for example, to write your code in one file and let the IDE write his code in another file.

About virtual and override, look at this other thread: Why do we use virtual and override?

Community
  • 1
  • 1
Salvatore Previti
  • 8,956
  • 31
  • 37