C# (pronounced “C-sharp”) is a modern, object-oriented programming language developed by Microsoft as part of its .NET initiative
C# is built around the principles of object-oriented programming, including classes, inheritance, polymorphism, and encapsulation.
Ensures that all operations are type-checked at compile time, reducing runtime errors and enhancing code reliability.
Automatic memory management through garbage collection, which helps in efficiently managing memory and preventing memory leaks.
Structured exception handling with try, catch, finally, and throw keywords to gracefully handle runtime errors.
Provides a powerful, integrated query syntax for querying collections, databases, XML, and more, making data manipulation more intuitive and readable.
async and await keywords simplify asynchronous programming, allowing developers to write more responsive and scalable applications.
In C#, the distinction between value types and reference types is fundamental to understanding how data is stored and managed in memory. Here’s a detailed explanation of the differences:
Value Types
When you assign one value type to another, a copy of the data is made. Modifications to one variable do not affect the other.
int x = 10;int y = x; // y gets a copy of xy = 20;// x is still 10
Reference Types
class MyClass{public int Value;}MyClass obj1 = new MyClass();obj1.Value = 10;MyClass obj2 = obj1; // obj2 references the same object as obj1obj2.Value = 20;// obj1.Value is now 20, because obj1 and obj2 refer to the same object
Namespaces in C# are used to organize code and prevent naming conflicts by grouping related classes, interfaces, enums, structs, and delegates under a unique name. Here are the main features and benefits of using namespaces in C#
Defining and Using Namespaces:
namespace MyApplication.Utilities{public class Logger{public void Log(string message){// Implementation here}}}
using MyApplication.Utilities;class Program{static void Main(){Logger logger = new Logger();logger.Log("Hello, World!");}}
namespace MyApplication{namespace Data{public class Database{// Implementation here}}namespace Services{public class AuthenticationService{// Implementation here}}}
Aliases and Fully Qualified Names:
using Auth = MyApplication.Services.AuthenticationService;class Program{static void Main(){Auth authService = new Auth();// Use authService here}}
class Program{static void Main(){MyApplication.Services.AuthenticationService authService = new MyApplication.Services.AuthenticationService();// Use authService here}}
In C#, a nullable type is a value type that can also represent null, in addition to its normal range of values. This is useful for value types, such as int, double, bool, etc., which by default cannot be null. Nullable types are particularly useful in database interactions, form inputs, and scenarios where a value might be optional or undefined.
Using Nullable
Using the shorthand T?:
int? nullableInt = null;
Example Usage
int? nullableInt = 5;if (nullableInt.HasValue){Console.WriteLine($"Value: {nullableInt.Value}");}else{Console.WriteLine("Value is null");}nullableInt = null;if (!nullableInt.HasValue){Console.WriteLine("Value is null");}
The null-coalescing operator (??) provides a convenient way to specify a default value if the nullable type is null:
int? nullableInt = null;int value = nullableInt ?? 0; // value will be 0 if nullableInt is null
The null-conditional operator (?.) allows you to safely access members and properties of nullable types without needing to check for null explicitly:
int? nullableInt = null;int length = nullableInt?.ToString().Length ?? 0; // length will be 0 if nullableInt is null
Quick Links
Legal Stuff
Social Media