Unit1 Part3
Unit1 Part3
Unit1 Part3
• Many developers need access to the same class, then having the class in
multiple files can be beneficial.
• The partial keywords allow a class to span multiple source files.
• A partial type must have the same accessibility.
• If the partial type is sealed or abstract then the entire class will be sealed and
abstract.
Advantages
• Multiple developers can work simultaneously in the same class in different files.
• When you were working with automatically generated code, the code can be added to the
class without having to recreate the source file like in Visual studio.
• You can also maintain your application in an efficient manner by compressing large
classes into small ones.
• With the help of a partial class concept, you can split the UI of the design code and the
business logic code to read and understand the code.
EXAMPLE
public partial class Employee
{
public void DoWork()
{
}
}
public partial class Employee
{
public void GoToLunch()
{
}
}
STATIC CLASS
}
CONTD…
Calculator c = new Calculator();
int sum = c.Add(5,6);
int sum2=c.Add(7,8,9);
EXAMPLE OF OVERRIDING
MessageBox.Show(p.Meth1());
MessageBox.Show(p.Meth2());
OUTPUT
• MyDerived-Meth1
• MyBase-Meth1
Generics
• C# has two separate mechanisms for writing code that is reusable across different types:
inheritance and generics.
• Whereas inheritance expresses reusability with a base type, generics express reusability
with a “template” that contains “placeholder” types.
• Generics, when compared to inheritance, can increase type safety and reduce casting and
boxing
Generic Types
• A generic type declares type parameters—placeholder types to be filled in by the
consumer of the generic type, which supplies the type arguments.
• Here is a generic type Stack<T>, designed to stack instances of type T. Stack<T> declares
a single type parameter T:
CONTD…
• Generics exist to write code that is reusable across different types.
class DataStore<T>
{
public T Data { get; set; }
}
DataStore<string> store = new DataStore<string>();