Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Important Keywords in C#
C# provides several important keywords that control class behavior, method accessibility, and parameter handling. These keywords include sealed, params, internal, this, and abstract, each serving specific purposes in object-oriented programming.
Sealed Keyword
The sealed keyword prevents a class from being inherited or a method from being overridden. When applied to a method, it must be an overridden method in a derived class.
Syntax
public sealed class ClassName { }
public sealed override void MethodName() { }
Example
using System;
class Animal {
public virtual void MakeSound() {
Console.WriteLine("Animal makes a sound");
}
}
class Dog : Animal {
public sealed override void MakeSound() {
Console.WriteLine("Dog barks");
}
}
// This would cause a compilation error:
// class Puppy : Dog {
// public override void MakeSound() { } // Error: cannot override sealed method
// }
sealed class FinalClass {
public void Display() {
Console.WriteLine("This is a sealed class");
}
}
// This would cause a compilation error:
// class DerivedClass : FinalClass { } // Error: cannot inherit from sealed class
class Program {
public static void Main() {
Dog dog = new Dog();
dog.MakeSound();
FinalClass final = new FinalClass();
final.Display();
}
}
The output of the above code is −
Dog barks This is a sealed class
Params Keyword
The params keyword allows a method to accept a variable number of arguments of the same type. It must be the last parameter in the method signature.
Syntax
public void MethodName(params type[] parameterName) { }
Example
using System;
class Calculator {
public int Sum(params int[] numbers) {
int total = 0;
foreach (int num in numbers) {
total += num;
}
return total;
}
public void DisplayNames(string prefix, params string[] names) {
Console.WriteLine(prefix);
foreach (string name in names) {
Console.WriteLine("- " + name);
}
}
}
class Program {
public static void Main() {
Calculator calc = new Calculator();
// Different ways to call params method
Console.WriteLine("Sum of 2, 3: " + calc.Sum(2, 3));
Console.WriteLine("Sum of 1, 2, 3, 4, 5: " + calc.Sum(1, 2, 3, 4, 5));
Console.WriteLine("Sum of no parameters: " + calc.Sum());
calc.DisplayNames("Students:", "Alice", "Bob", "Charlie");
}
}
The output of the above code is −
Sum of 2, 3: 5 Sum of 1, 2, 3, 4, 5: 15 Sum of no parameters: 0 Students: - Alice - Bob - Charlie
Internal Access Modifier
The internal access modifier makes a class member accessible within the same assembly but not from other assemblies.
Syntax
internal class ClassName { }
internal int fieldName;
internal void MethodName() { }
Example
using System;
internal class DatabaseConnection {
internal string connectionString = "Server=localhost;Database=MyDB;";
internal void Connect() {
Console.WriteLine("Connected to: " + connectionString);
}
private void CloseConnection() {
Console.WriteLine("Connection closed");
}
}
public class DataService {
internal int recordCount;
public void ProcessData() {
DatabaseConnection db = new DatabaseConnection();
db.Connect(); // Can access internal method within same assembly
recordCount = 100;
Console.WriteLine("Processed " + recordCount + " records");
}
}
class Program {
public static void Main() {
DataService service = new DataService();
service.ProcessData();
Console.WriteLine("Record count: " + service.recordCount);
}
}
The output of the above code is −
Connected to: Server=localhost;Database=MyDB; Processed 100 records Record count: 100
This Keyword
The this keyword refers to the current instance of the class and is used to differentiate between method parameters and class fields when they have the same name.
Syntax
this.fieldName = parameterName; this.MethodName();
Example
using System;
class Employee {
private string name;
private int age;
private double salary;
public Employee(string name, int age, double salary) {
this.name = name; // this.name refers to the field
this.age = age; // age parameter assigns to this.age field
this.salary = salary;
}
public void UpdateSalary(double salary) {
this.salary = salary; // Distinguishes field from parameter
Console.WriteLine(this.name + "'s salary updated to: $" + this.salary);
}
public void DisplayInfo() {
Console.WriteLine("Name: " + this.name + ", Age: " + this.age + ", Salary: $" + this.salary);
}
}
class Program {
public static void Main() {
Employee emp = new Employee("John Doe", 30, 50000);
emp.DisplayInfo();
emp.UpdateSalary(55000);
}
}
The output of the above code is −
Name: John Doe, Age: 30, Salary: $50000 John Doe's salary updated to: $55000
Abstract Keyword
The abstract keyword creates abstract classes and methods. Abstract classes cannot be instantiated and may contain both abstract and non-abstract methods.
Syntax
public abstract class ClassName {
public abstract void AbstractMethod();
public void ConcreteMethod() { }
}
Example
using System;
abstract class Shape {
protected string color;
public Shape(string color) {
this.color = color;
}
public abstract double CalculateArea();
public void DisplayColor() {
Console.WriteLine("Color: " + color);
}
}
class Circle : Shape {
private double radius;
public Circle(string color, double radius) : base(color) {
this.radius = radius;
}
public override double CalculateArea() {
return Math.PI * radius * radius;
}
}
class Rectangle : Shape {
private double length, width;
public Rectangle(string color, double length, double width) : base(color) {
this.length = length;
this.width = width;
}
public override double CalculateArea() {
return length * width;
}
}
class Program {
public static void Main() {
Shape circle = new Circle("Red", 5);
Shape rectangle = new Rectangle("Blue", 4, 6);
circle.DisplayColor();
Console.WriteLine("Circle Area: " + circle.CalculateArea().ToString("F2"));
rectangle.DisplayColor();
Console.WriteLine("Rectangle Area: " + rectangle.CalculateArea());
}
}
The output of the above code is −
Color: Red Circle Area: 78.54 Color: Blue Rectangle Area: 24
Keyword Comparison
| Keyword | Purpose | Usage Context |
|---|---|---|
| sealed | Prevents inheritance/overriding | Classes and override methods |
| params | Variable number of arguments | Method parameters |
| internal | Assembly-level access | Classes, methods, fields |
| this | Current instance reference | Instance members |
| abstract | Defines incomplete classes/methods | Classes and methods |
Conclusion
These five keywords provide essential control over class behavior in C#. The sealed keyword prevents inheritance, params enables flexible parameter lists, internal controls assembly access, this references the current instance, and abstract defines incomplete classes that require implementation in derived classes.
