1/16
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
What is a Method in C#?
A method is a code block that contains a series of statements.
What is the difference between Pass by Value and Pass by Reference Parameters?
The difference lies in how arguments are passed to methods; pass by value creates a copy of the variable, while pass by reference passes a reference to the original variable, allowing for modifications.
How to return more than one value from a method in C#?
By using ref and out keywords
By using a List, Arraylist(any collection) in the return type.
What is the difference between “out” and “ref” parameters?
Use out parameter to return a new and fresh value and use ref to modify an existing value.
Rules for ref vs out
1. No need to initialize out parameter before passing it.
2. Out parameter must be initialized before returning.
1. Must initialize ref parameter else error.
2. For Ref parameter, Initialization is not necessary before returning.
What is “params” keyword?
Params keyword is used as a parameter which can take the VARIABLE number of parameters.
Its basically C# version for rest operator in js
When to use params keyword in real applications?
It is useful when programmer don’t have any prior knowledge about the number of parameters to be used.
What are optional parameters in a method?
Optional parameters allow some arguments which are not mandatory to pass, and their default value is set.
EX:
void Greet(string name = "Guest") {
Console.WriteLine($"Hello, {name}!");
}
// You can call it with or without the argument:
Greet(); // Output: Hello, Guest!
Greet("William"); // Output: Hello, William!
What are named parameters in a method?
Named parameters are used to specify an argument based on the name of the argument and not the position.
EX:
void Greet(string name, int age) {
Console.WriteLine($"Hello {name}, you are {age} years old.");
}
// Using named parameters:
Greet(age: 30, name: "William");
What are Extension Methods in C#?
Extension method allows you to add new methods in the existing class without modifying the source code of the original class.
EX:
public static class StringExtensions {
public static bool IsCapitalized(this string input) {
return !string.IsNullOrEmpty(input) && char.IsUpper(input[0]);
}
}
// Usage:
string name = "William";
bool result = name.IsCapitalized(); // Returns true
Rules for Extension Method
Extension method must be static because this will be directly called from the class name, not by the object creation.
this keyword is used for binding this method with the main class.
When to use extension methods in real applications?
USE - Use them when you want to add a method in a class which code you don’t have.
Ex: Strings
What are Delegates in C#?
A Delegate is a variable that holds the reference to a method or Pointer to a method.
A delegate can refer to more than one methods of same return type and parameters.
When to use delegates in real applications?
When we need to pass a method as a parameter.
EX: Logs with different logic for a string parameter
What are Multicast Delegates?
A Multicast Delegate in C# is a delegate that holds the references of more than one function.
using System;
delegate void Calculator(int x, int y);
class Program
{
public static void Add(int a, int b)
{
Console.WriteLine(a + b);
}
public static void Mul(int a, int b)
{
Console.WriteLine(a * b);
}
static void Main()
{
Calculator calc = Add;
calc += Mul;
calc(20, 30); // Output: 50 600
}
}
What are Anonymous Delegates in C#?
Delegates pointing methods without name are called anonymous delegates
using System;
public delegate void Calculator(int x, int y);
class Program
{
static void Main()
{
Calculator calcAdd = delegate(int a, int b) {
Console.WriteLine(a + b);
};
calcAdd(20, 30); // Output: 50
}
}
What are the differences between Events and Delegates?
The event is a notification mechanism that depends on delegates
Functions → Delegate → Event
An event is dependent on a delegate and cannot be created without delegates.
Event is like a wrapper over the delegate to improve its security.