C# Classes and Object-Oriented Programming

C# is a statically-typed, object-oriented programming language. It offers a range of tools to encapsulate, inherit, and interface different components of a software system.

Interfaces

Interfaces define a contract for classes and structs. An interface can have methods, properties, events, and indexers, but no implementation. Classes and structs inheriting from an interface must provide an implementation for all its members.


                            public interface IAnimal {
                                void Speak();
                            }

                            public class Dog : IAnimal {
                                public void Speak() {
                                    Console.WriteLine("Woof!");
                                }
                            }
                        

Abstract Classes

Abstract classes serve as a base for other classes. They can't be instantiated directly. Instead, other classes must derive from them and implement any abstract members. Abstract classes can have a mix of abstract (no implementation) and concrete (with implementation) members.


                            public abstract class Animal {
                                public abstract void Speak();

                                public void Move() {
                                    Console.WriteLine("Animal moves");
                                }
                            }

                            public class Cat : Animal {
                                public override void Speak() {
                                    Console.WriteLine("Meow!");
                                }
                            }
                        

Internal Classes

Internal classes are accessible only within the assembly (or project) they reside in. They are commonly used to encapsulate classes that are intended to be used exclusively within the same assembly, hiding their details from other assemblies.


                            internal class InternalClass {
                                public void Show() {
                                    Console.WriteLine("This is an internal class method.");
                                }
                            }

                            // This class can be accessed only within the same assembly
                        

Nested Classes

A nested class is a class defined within another class. The primary class is referred to as the outer class, while the class defined within it is referred to as the nested, or inner, class. Nested classes can be public, private, protected, or internal.


                            public class OuterClass {
                                private int outerVariable;

                                public class InnerClass {
                                    public void Display() {
                                        Console.WriteLine("This is a nested class method.");
                                    }
                                }
                            }

                            // Usage:
                            // OuterClass.InnerClass inner = new OuterClass.InnerClass();
                        

Static Classes

Static classes are sealed by default and cannot be instantiated. They are generally used to create utility functions. All members of a static class should be static.


                            public static class MathUtility {
                                public static int Add(int a, int b) {
                                    return a + b;
                                }
                            }

                            // Usage:
                            // int result = MathUtility.Add(5, 3);
                        

Encapsulation

Encapsulation ensures that the internal representation of an object is hidden from the outside. In C#, this is achieved using access modifiers like private, protected, and public.

A more advance Employee and Employee Functions class in argument exceptions can be found here Employee


                            public class Circle {
                                private double radius;

                                public Circle(double radius) {
                                    this.radius = radius;
                                }

                                public double GetArea() {
                                    return 3.14 * radius * radius;
                                }
                            }
                        

Inheritance

Inheritance allows a class to inherit properties and methods from another class. In C#, a class can only inherit from one other class.


                            public class Vehicle {
                                public void Drive() {
                                    Console.WriteLine("Driving the vehicle");
                                }
                            }

                            public class Car : Vehicle {
                                public void Honk() {
                                    Console.WriteLine("Honk honk!");
                                }
                            }
                        

Polymorphism

Polymorphism allows you to invoke derived class methods through a base class reference. This provides the flexibility to point the base class reference at any derived class object.


                            public class Shape {
                                public virtual void Draw() {
                                    Console.WriteLine("Drawing a shape");
                                }
                            }

                            public class Rectangle : Shape {
                                public override void Draw() {
                                    Console.WriteLine("Drawing a rectangle");
                                }
                            }
                        

Sealed Classes

Sealed classes are used to restrict the inheritance feature of object-oriented programming. Once a class is defined as sealed, it cannot be inherited. In C#, the sealed modifier is used to define a class as sealed.

Usage of Sealed Classes

Sealed classes are primarily used when you want to prevent further extension of a class hierarchy, usually for security or versioning reasons. For example, many of the built-in .NET classes are sealed.


                            public sealed class SealedClass {
                                public void Display() {
                                    Console.WriteLine("This is a sealed class method.");
                                }
                            }
        
                            // The following will raise a compile-time error
                            // public class DerivedClass : SealedClass { }