In C#, a class is a blueprint or template for creating objects (instances). It defines the properties (variables) and methods (functions) that the objects created from the class will have. Classes allow you to structure data and behavior together in a way that is easy to manage and reuse.
class Person
{
public string name;
public void ShowInfo()
{
Console.WriteLine("Name: " + name);
}
}
class Program
{
static void Main(string[] args)
{
Person p1 = new Person(); // create instance
p1.ShowInfo();
}
}
class Person
{
public string Name;
public int Age;
public Person()
{
Name = "No Name";
Age = 0;
Console.WriteLine("constructor activates");
}
public void ShowInfo()
{
Console.WriteLine($"Name: {Name}, Age: {Age}");
}
}
class Program
{
static void Main(string[] args)
{
Person p1 = new Person(); // create instance
p1.ShowInfo();
}
}
class Person
{
public string Name;
public int Age;
public Person(string name, int age)
{
Name = name;
Age = age;
Console.WriteLine("constructor w/ parameter activates");
}
public void ShowInfo()
{
Console.WriteLine($"Name: {Name}, Age: {Age}");
}
}
class Program
{
static void Main(string[] args)
{
Person p1 = new Person("Tom", 25); // create instance
p1.ShowInfo();
Person p2 = new Person("Lisa", 30);
p2.ShowInfo();
}
}
The this keyword refers to the current instance of the class. It is useful when distinguishing between instance variables and parameters, or when calling other constructors within the same class.
public class Car
{
public string Make;
public string Model;
public int Year;
// Constructor using 'this' to reference instance variables
public Car(string Make, string Model, int Year)
{
this.Make = Make; // 'this.Make' refers to the instance variable, while 'Make' is the parameter
this.Model = Model;
this.Year = Year;
}
public void DisplayInfo()
{
Console.WriteLine($"Car: {this.Year} {this.Make} {this.Model}");
}
}
class Program
{
static void Main(string[] args)
{
Car myCar1 = new Car("Honda", "Civic"); // Uses default year 2000
Car myCar2 = new Car("Ford", "Mustang", 2024);
myCar1.DisplayInfo(); // Output: Car: 2000 Honda Civic
myCar2.DisplayInfo(); // Output: Car: 2024 Ford Mustang
}
}
Destructor is a special method that is called when an object is destroyed or garbage collected. It is used to release unmanaged resources like file handles, database connections, or network sockets before the object is removed from memory.
class Example
{
// Constructor
public Example()
{
Console.WriteLine("Object Created");
}
// Destructor
~Example()
{
Console.WriteLine("Object Destroyed");
}
}
class Program
{
static void Main()
{
Example obj = new Example();
} // Destructor will be called when obj is collected by GC
}
get and set methods are used to encapsulate class fields, allowing controlled access to them. This is done using properties, which act as a bridge between private fields and external access.
// set value (setter)
public void SetName(string newName)
{
name = newName;
}
// get value (getter)
public string GetName()
{
return name;
}
// shorter method #1
public string Name
{
get { return name; } // getter
set { name = value; } // setter
}
// shorter method #2 (most used)
public string Name
{
public string Name { get; set; }
}
// Example
class Person
{
private int count = 100; // Private field with an initial value of 100
public string Name { get; set; } // Auto-implemented property (can be read and written)
public int Count
{
get { return count; } // Read-only property (only has a getter)
}
public float Balance { get; private set; } // Public getter, private setter (can only be modified inside the class)
public void AddBalance()
{
Balance += 100; // Increases Balance by 100
}
}
class Program
{
static void Main(string[] args)
{
Person p = new Person(); // Create a new Person object
p.Name = "Tom"; // Set the Name property
p.AddBalance(); // Increase balance by 100
// Print the values
Console.WriteLine("Name: " + p.Name + " Count: " + p.Count + " Balance: " + p.Balance);
}
}
Environment is a static class in the System namespace that provides information about the runtime environment, system settings, and operations related to the system.
You can use it to get details about the operating system, machine, and application runtime.
Console.WriteLine("Terminate Program");
string path = Environment.GetEnvironmentVariable("PATH");
Console.WriteLine($"PATH: {path}");
Environment.Exit(0); // program ends automatically
Random class in C# is used to generate random numbers.
Random random = new Random();
int randomNumber = random.Next(1, 101);
Console.WriteLine("Random Number: " + randomNumber);
Stopwatch class in C# (from the System.Diagnostics namespace) is used to measure elapsed time with high precision. It’s useful for timing operations, measuring performance, and controlling game loops.
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start(); // Start measuring time
System.Threading.Thread.Sleep(1000); // Simulate some work (1 second delay)
stopwatch.Stop(); // Stop measuring time
Console.WriteLine($"Elapsed Time: {stopwatch.ElapsedMilliseconds} ms"); // Output: ~1000 ms
A Regular Expression (Regex) is a pattern-matching tool used for searching, validating, and manipulating text.
string input = "Hello, my phone number is 010-1234-5678.";
string pattern = @"\d{3}-\d{4}-\d{4}"; // phone number pattern
bool isMatch = Regex.IsMatch(input, pattern);
Console.WriteLine($"Phone Number Exist? {isMatch}"); // true
static void Increase(ref int x)
{
x++;
}
static void OutFunc(int a, int b, out int x, out int y)
{
x = a;
y = b;
}
static void Main(string[] args)
{
int a = 10;
int b = 20;
Increase(ref a);
Console.WriteLine("Value A: " + a); // value connects without return
int x, y;
OutFunc(a, b, out x, out y);
Console.WriteLine("x: " + x + " y: " + y);
}