Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 9

Introduction to

Constructors, Partial
Classes, and Static Classes
Constructors
Constructors are special methods in a class that are used to initialize the object. They have the same name as
the class and do not have a return type. Constructors are automatically called when an object is created.
They can be used to set initial values for the object's properties or perform any necessary setup tasks.

public class MyClass


{
public MyClass()
{
// Constructor code
}
}
Constructor Overloading
Constructor overloading allows multiple public class MyClass
constructors with different parameter lists in a {
class. public MyClass(int value)
 It enables creating objects with different {
// Constructor code with int
initialization options.
parameter
 By providing different constructors, you can }
handle different scenarios and provide
flexibility to the users of your class. public MyClass(string text)
 The appropriate constructor is called based {
on the arguments provided when creating an // Constructor code with string
object. parameter
}
}
To avoid code duplication, one constructor may call another, using the
this keyword:

using System;
public class Wine
{
public decimal Price;
public int Year;
public Wine (decimal price) { Price = price; }
public Wine (decimal price, int year) : this (price)
{
Year = year;
}
}

When one constructor calls another, the called


constructor executes first
Destructor
Destructors, also known as finalizers, are used to perform
cleanup tasks before an object is destroyed or garbage-
collected.
public class MyClass  In C#, destructors are defined using the tilde (~)
{ followed by the class name.
~MyClass()  Destructors are called automatically by the garbage
{
collector when an object is no longer in use.
// Destructor code
}  It is recommended to implement the IDisposable
} interface for managing resources explicitly.
Partial Class File 1 (MyClass.cs):
Partial classes allow dividing the definition of a
public partial class MyClass
class into multiple files. {
 Each file contains a part of the class definition, // Code for MyClass
using the 'partial' keyword. }
 This feature is useful when working with large
classes or when multiple developers are
File 2 (MyClassPart.cs):
working on the same class.
 All the parts of the class are combined at
public partial class MyClass
compile-time to create a single class {
definition. // Code for MyClass (continued)
}
Static Classes
Static classes are classes that cannot be instantiated
and can only contain static members.
 Static members are shared among all instances public static class MyStaticClass
{
of the class and can be accessed without public static void MyStaticMethod()
creating an object. {
 They are typically used to define utility or // Static method code
helper functions that do not require any state. }
 Static classes are declared using the 'static' }
keyword.
Static Properties
Static properties belong to the class itself, rather
than to individual objects.
 They are accessed using the class name,
followed by the property name. public class MyStaticClass
{
 Static properties are commonly used to store public static int MyStaticProperty { get; set; }
global or shared data. }
 They can be read-only or read-write,
depending on the requirements.
 Static properties are defined using the 'static'
keyword.
Static Methods
Static methods are methods that belong to the class itself, rather
than to individual objects.
 They can be called directly using the class name, without
public class MyStaticClass creating an instance of the class.
{  Static methods are useful for performing tasks that do not
public static void MyStaticMethod() require accessing instance-specific data.
{  They are defined using the 'static' keyword.
// Static method code
}
}

You might also like