Methods
What are the Methods?
Methods are blocks of code that perform a specific task. They can accept input parameters, execute a sequence of statements, and optionally return a value. Methods help in organizing and reusing code.
Defining Methods
A method definition includes a return type, a method name, a parameter list (optional), and a body enclosed in braces.
Syntax
returnType MethodName(parameters)
{
// Method body
}
Example
public class Calculator
{
// Method to add two numbers
public int Add(int a, int b)
{
return a + b;
}
// Method to multiply two numbers
public int Multiply(int a, int b)
{
return a * b;
}
}
Calling Methods
Methods are called using the object of the class in which they are defined, or directly if they are static.
Example
public class Program
{
public static void Main(string[] args)
{
Calculator calc = new Calculator();
int sum = calc.Add(5, 3);
int product = calc.Multiply(5, 3);
Console.WriteLine("Sum: " + sum);
Console.WriteLine("Product: " + product);
}
}
Method Overloading
Method overloading allows you to define multiple methods with the same name but different parameter lists. The compiler determines which method to call based on the arguments passed.
Example
public class Printer
{
// Method to print an integer
public void Print(int number)
{
Console.WriteLine("Number: " + number);
}
// Method to print a string
public void Print(string message)
{
Console.WriteLine("Message: " + message);
}
}
Static Methods
Static methods belong to the class itself rather than an instance of the class. They are called using the class name.
Example
public class MathUtilities
{
public static int Square(int number)
{
return number * number;
}
}
public class Program
{
public static void Main(string[] args)
{
int square = MathUtilities.Square(4);
Console.WriteLine("Square: " + square);
}
}
Properties
What are Properties?
Properties are members of a class that provide a flexible mechanism to read, write, or compute the values of private fields. They are a combination of methods that allow you to access and update the value of a field in a controlled manner.
Defining Properties
A property definition includes a get accessor and/or a set accessor.
Syntax
accessModifier dataType PropertyName
{
get { // Return the value of the field }
set { // Set the value of the field }
}
Example
public class Person
{
private string name;
// Property to get and set the name
public string Name
{
get { return name; }
set { name = value; }
}
}
Auto-Implemented Properties
For simple properties that do not require additional logic in the accessors, you can use auto-implemented properties.
Example
public class Employee
{
// Auto-implemented property
public int ID { get; set; }
public string Name { get; set; }
}
Read-Only Properties
A read-only property has only a get accessor and can only be assigned a value in the constructor or directly in the property definition.
Example
public class Car
{
// Read-only property
public string Model { get; }
public Car(string model)
{
Model = model;
}
}
Computed Properties
Properties can also compute values dynamically based on other fields or properties.
Example
public class Car
{
// Read-only property
public string Model { get; }
public Car(string model)
{
Model = model;
}
}
Conclusion
Understanding methods and properties in C# is crucial for writing clean, maintainable, and efficient code. Methods allow you to encapsulate functionality, promote code reuse, and improve readability. Properties provide a flexible way to encapsulate data access, ensuring that fields are protected while still allowing controlled read and write operations.
By mastering these concepts, you will be well-equipped to create robust object-oriented applications in C#.