Params:
In C#, params is a keyword that lets you pass a variable number of
arguments to a method. It is especially useful when you don’t know how
many inputs a user might provide.
Allows you to send 0 or more values to a method.
Only one params parameter is allowed in a method.
It must be the last parameter in the method signature.
Internally, a params argument is treated as an array.
using System;
class Program
{
static void Main()
{
Sum(10, 20);
Sum(1, 2, 3, 4, 5);
Sum(); // also allowed
show(1);
show(1,55,67);
}
static void show(int n, params int[] a)
{}
static void Sum(params int[] nums)
{
int total = 0;
foreach (int n in nums)
{
total += n;
}
[Link]("Sum = " + total);
}
}
Ref & Out:
In C#, ref and out are both used to pass arguments by reference instead of
by value — but they have important differences.
ref means the variable must be initialized before passing it to the method,
and the method can read and modify it.
out means the variable does NOT need to be initialized before passing,but
the method must assign a value before returning.
using System;
class Program
{
static void Main()
{
int a = 5;
int b;
RefExample(ref a);
OutExample(out b);
[Link]("ref: " + a); // 50
[Link]("out: " + b); // 100
}
static void RefExample(ref int x)
{
x *= 10;
}
static void OutExample(out int y)
{
y = 100; // must assign
}
}
Output tracing
using System;
class Program
{
static int Sum(params int[] numbers)
{
int total = 0;
foreach (int n in numbers)
{
total += n;
}
return total;
}
static void DoubleValue(ref int x)
{
x = x * 2;
}
static void Divide(int a, int b, out int quotient, out int remainder)
{
quotient = a / b;
remainder = a % b;
}
static void Main()
{
int result = Sum(10, 20, 30, 40);
[Link]("Sum using params: " + result);
int value = 5;
DoubleValue(ref value);
[Link]("Value after ref: " + value);
int q, r;
Divide(20, 3, out q, out r);
[Link]("Quotient: " + q + ", Remainder: " + r);
}
}
Output:
Sum using params: 100
Value after ref: 10
Quotient: 6, Remainder: 2
Feature ref out
Must be initialized before
✔ Yes ✘ No
calling?
Must be assigned inside
✘ No ✔ Yes
method?
Updating Returning multiple
Used mainly for
values values
✘ No (initial value
Reads original value? ✔ Yes
ignored)
Readonly:
In C#, a readonly variable is a field whose value can be assigned only once—
either at the point of declaration or inside a constructor. After that, it cannot
be changed.
It is different from const, which must be assigned at compile time and is
always static.
Assigned only once: At declaration or inside a constructor
Can be instance-specific (non-static) or static.
Value can be set at runtime (unlike const which is compile-time fixed).
Cannot be modified later, ensures immutability.
class MyClass
{
public readonly int myValue = 10; // assigned at declaration
public readonly int anotherValue;
public MyClass(int val)
{
anotherValue = val; // assigned in constructor
}
public void ChangeValue()
{
// myValue = 20; // Error: readonly field cannot be assigned
}
}
Const:
In C#, the const keyword is used to declare a compile-time constant—a value
that cannot change after it is [Link] is implicitly static, meaning it belongs
to the type, not an instance, and must be initialized at the time of
declaration.
Must be initialized at declaration.
Value cannot change after compilation.
Implicitly static → belongs to the class, not instances.
Can only use simple types (primitive types, enums, strings, etc.) that
are known at compile time.
Cannot be assigned in constructor (unlike readonly).
using System;
class Program
{
class Circle
{
public const double Pi = 3.14159;
public static double Area(double radius)
{
return Pi * radius * radius;
}
}
static void Main()
{
double area = [Link](5);
[Link]("Area = " + area); // Area = 78.53975
// [Link] = 3.14; // Error: const cannot be changed
}
}
Feature const readonly
When assigned Must be at declaration At declaration or in constructor
Can change later No No
Runtime vs Runtime (can assign at runtime
Compile-time constant
Compile-time in constructor)
Static Implicitly static Optional
Only primitive types, Any type, including reference
Types allowed
enums, string types
new- runtime polymorphism:
The new keyword is used when a derived class defines a method with the
same name as a base class method without overriding it. This is called
method hiding.
using System;
class Base
{
public void Show()
{
[Link]("Base Show");
}
public virtual void Display()
{
[Link]("Base Display");
}
}
class Derived : Base
{
public new void Show() // hides [Link]
{
[Link]("Derived Show");
}
public override void Display() // overrides [Link]
{
[Link]("Derived Display");
}
}
class Program
{
static void Main()
{
Base b = new Derived();
Derived d = new Derived();
[Link](); // Base Show (method hiding depends on reference type)
[Link](); // Derived Display (run-time polymorphism)
[Link](); // Derived Show
[Link](); // Derived Display
}
}
Static constructor:
A static constructor in C# is a special type of constructor used to initialize
static members of a class. It is called automatically before the first instance
is created or any static members are referenced. You don’t call it explicitly.
Here’s a detailed explanation:
Key Features of a Static Constructor
1. No Access Modifiers
A static constructor cannot have an access modifier (public, private,
etc.). It is always private in effect.
2. No Parameters
It cannot take parameters. This distinguishes it from instance
constructors.
3. Called Automatically
The runtime calls it once per type, and only before the first use of the
class.
4. Used for Static Initialization
Typically used to initialize static fields or perform one-time setup.
5. Cannot be Called Directly
You cannot call a static constructor explicitly in code.
class MyClass
{
public static int count;
// Static constructor
static MyClass()
{
count = 100;
[Link]("Static constructor called");
}
// Instance constructor
public MyClass()
{
[Link]("Instance constructor called");
}
}
class Program
{
static void Main(string[] args)
{
[Link]([Link]); // Static constructor called here
MyClass obj1 = new MyClass(); // Instance constructor called
}
}
using System;
namespace Example
{
enum Status
{
Success,
Failure,
Unknown
}
class Program
{
static void Main(string[] args)
{
int[][] jaggedArray = new int[3][];
jaggedArray[0] = new int[] { 1, 2 };
jaggedArray[1] = new int[] { 3, 4, 5 };
jaggedArray[2] = new int[] { 6, 7, 8, 9 };
[Link]("Jagged Array Contents:");
PrintJaggedArray(jaggedArray);
int a = 5, b;
Status result = AddNumbers(ref a, 10, out b);
[Link]($"\nAfter AddNumbers: a = {a}, b = {b}, Status =
{result}");
}
static void PrintJaggedArray(int[][] array)
{
for (int i = 0; i < [Link]; i++)
{
[Link]("Row " + i + ": ");
for (int j = 0; j < array[i].Length; j++)
{
[Link](array[i][j] + " ");
}
[Link]();
}
}
static Status AddNumbers(ref int x, int y, out int sum)
{
try
{
x += y;
sum = x + y;
return [Link];
}
catch
{
sum = 0;
return [Link];
}
}
}
}
Jagged Array Contents:
Row 0: 1 2
Row 1: 3 4 5
Row 2: 6 7 8 9
After AddNumbers: a = 15, b = 25, Status = Success