Types of Classes
In C#, classes can be categorized into various types based on their functionality and usage. Here are some common types of classes in C#:
- Standard or Concrete Classes
- Abstract Classes
- Static Classes
- Sealed Classes
- Partial Classes
- Nested Classes
- Generic Classes
- Anonymous Classes
- Partial Methods
- Extension Methods
Standard or Concrete Classes: These are the most common types of classes used in C#. They define objects with specific attributes and behaviors. Examples include classes for modeling entities in a system, such as
Person,Car, orEmployee.Abstract Classes: Abstract classes cannot be instantiated directly; they serve as base classes for other classes. They may contain abstract methods that must be implemented by derived classes. Abstract classes are declared using the
abstractkeyword.Static Classes: Static classes cannot be instantiated, and they can only contain static members (fields, methods, properties). They are commonly used to define utility classes that provide helper functions or constants. Static classes are declared using the
statickeyword.Sealed Classes: Sealed classes cannot be inherited. They are often used to prevent further derivation or overriding of functionality. Sealed classes are declared using the
sealedkeyword.Partial Classes: Partial classes allow a class's members to be split into multiple files. This is useful for separating auto-generated code from hand-written code, or for organizing large classes into manageable parts. Partial classes are declared using the
partialkeyword.Nested Classes: Nested classes are classes defined within another class. They can access the private members of the enclosing class and are often used for encapsulation or to logically group related functionality. Nested classes can be static or non-static.
Generic Classes: Generic classes are parameterized types that can work with any data type. They allow you to create classes, structures, interfaces, and methods with placeholders for data types, making them highly reusable and flexible.
Anonymous Classes: Anonymous classes are created dynamically without explicitly defining a class type. They are typically used in LINQ queries or when returning multiple values from a method.
Partial Methods: Partial methods are methods declared in a partial class or a partial struct that may or may not have an implementation. Partial methods with no implementation are automatically removed at compile time if not implemented by any part of the class or struct.
Extension Methods: Extension methods allow you to add new methods to existing types without modifying the original type's source code. They are declared as static methods in static classes and must be in the same namespace as the extended type or included via a
usingdirective.
These are some common types of classes in C#, each serving different purposes and providing various features to support object-oriented programming principles and design patterns.
EXamples:
- Standard or Concrete Class:
csharppublic class Person
{
public string Name { get; set; }
public int Age { get; set; }
public void SayHello()
{
Console.WriteLine($"Hello, my name is {Name} and I am {Age} years old.");
}
}
- Abstract Class:
csharppublic abstract class Shape
{
public abstract double Area();
public abstract double Perimeter();
}
public class Rectangle : Shape
{
public double Width { get; set; }
public double Height { get; set; }
public override double Area() => Width * Height;
public override double Perimeter() => 2 * (Width + Height);
}
- Static Class:
csharppublic static class MathHelper
{
public static double Square(double number) => number * number;
public static double Cube(double number) => number * number * number;
}
- Sealed Class:
csharppublic sealed class FinalClass
{
// Class implementation
}
- Partial Class:
csharp// File 1
public partial class PartialExample
{
public void Method1() { /* Implementation */ }
}
// File 2
public partial class PartialExample
{
public void Method2() { /* Implementation */ }
}
- Nested Class:
csharppublic class OuterClass
{
private int outerField;
public class NestedClass
{
public void InnerMethod(OuterClass outer)
{
outer.outerField = 10;
}
}
}
- Generic Class:
csharppublic class GenericList<T>
{
private List<T> items = new List<T>();
public void Add(T item) => items.Add(item);
public void Remove(T item) => items.Remove(item);
}
- Anonymous Class:
csharpvar person = new { Name = "John", Age = 30 };
Console.WriteLine($"Name: {person.Name}, Age: {person.Age}");
- Partial Method:
csharppublic partial class PartialMethodExample
{
partial void PartialMethod();
public void CallPartialMethod()
{
PartialMethod();
}
}
public partial class PartialMethodExample
{
partial void PartialMethod()
{
Console.WriteLine("Partial method implementation");
}
}
- Extension Method:
csharppublic static class StringExtensions
{
public static bool IsPalindrome(this string str)
{
string reversed = new string(str.Reverse().ToArray());
return str.Equals(reversed, StringComparison.OrdinalIgnoreCase);
}
}
// Usage:
string word = "radar";
bool isPalindrome = word.IsPalindrome();
Comments
Post a Comment