Methods and Functions
40 minMethods in C# can be static (belonging to the class) or instance methods (belonging to an object instance). Static methods are called on the class (e.g., Math.Max()), while instance methods are called on objects. Static methods cannot access instance members. Understanding method types enables proper method design. Method types affect how methods are called and what they can access.
C# supports method overloading, optional parameters, and named arguments, providing flexibility in method design. Overloading enables multiple methods with the same name but different parameters. Optional parameters have default values and can be omitted. Named arguments enable specifying parameters by name. Understanding these features enables flexible APIs. These features improve method usability.
The 'out' and 'ref' keywords allow methods to return multiple values or modify parameters passed by reference. 'out' parameters must be assigned in the method and don't need initialization. 'ref' parameters can be read and modified and must be initialized. Understanding out/ref enables multiple return values. out/ref are useful for methods that need to return multiple values.
Expression-bodied members provide concise syntax for simple methods, properties, and indexers using the => operator. Expression-bodied methods are ideal for single-expression methods. They reduce boilerplate code. Understanding expression-bodied members enables concise code. Expression-bodied members are modern C# syntax.
Method parameters can be passed by value (default), by reference (ref), or as output (out). Value parameters create copies. ref parameters enable modifying the original. out parameters are for returning values. Understanding parameter passing enables effective method design. Parameter passing affects method behavior.
Best practices include using meaningful method names, keeping methods focused (single responsibility), using optional parameters for convenience, avoiding too many parameters (consider objects), and documenting complex methods. Understanding methods enables building reusable code. Methods are fundamental to code organization.
Key Concepts
- Methods can be static (class-level) or instance (object-level).
- C# supports method overloading, optional parameters, and named arguments.
- out and ref keywords enable multiple return values or parameter modification.
- Expression-bodied members provide concise syntax for simple methods.
- Parameters can be passed by value, by reference (ref), or as output (out).
Learning Objectives
Master
- Creating static and instance methods
- Using method overloading and optional parameters
- Working with out and ref parameters
- Writing expression-bodied methods
Develop
- Understanding method design principles
- Designing flexible, reusable methods
- Appreciating methods' role in code organization
Tips
- Use static methods for utility functions that don't need instance data.
- Use optional parameters for convenience, but don't overuse them.
- Use out parameters when you need to return multiple values.
- Use expression-bodied members for simple, single-expression methods.
Common Pitfalls
- Creating methods that are too long, violating single responsibility.
- Using too many parameters, making methods hard to use.
- Confusing ref and out, causing compilation errors.
- Not documenting complex methods, making them hard to understand.
Summary
- Methods can be static or instance.
- C# supports overloading, optional parameters, and named arguments.
- out and ref enable multiple return values.
- Expression-bodied members provide concise syntax.
- Understanding methods enables code organization.
Exercise
Create methods with different parameter types and return values.
using System;
class Program
{
// Simple method
static void Greet(string name)
{
Console.WriteLine($"Hello, {name}!");
}
// Method with return value
static int Add(int a, int b)
{
return a + b;
}
// Method with out parameter
static bool TryDivide(int dividend, int divisor, out double result)
{
if (divisor == 0)
{
result = 0;
return false;
}
result = (double)dividend / divisor;
return true;
}
// Expression-bodied method
static int Square(int x) => x * x;
static void Main(string[] args)
{
Greet("Alice");
int sum = Add(5, 3);
Console.WriteLine($"5 + 3 = {sum}");
if (TryDivide(10, 2, out double quotient))
{
Console.WriteLine($"10 / 2 = {quotient}");
}
Console.WriteLine($"Square of 4: {Square(4)}");
}
}