History
- **How**:
- Developed by **Anders Hejlsberg** and his team at **Microsoft** in the early 2000s.
- Designed as a modern, object-oriented language that combines the power of C++ with the ease of use of languages like Java.
- Part of the **.NET Framework**, C# was intended to be a language for building a wide variety of applications, from desktop software to web applications and games.
- Evolved through versions, with notable milestones such as C# 2.0 (introducing generics), C# 3.0 (LINQ), and C# 5.0 (asynchronous programming with async/await).
-
- **Who**:
- **Anders Hejlsberg**, a Danish software engineer, known for creating C# and also the architect behind Turbo Pascal and Delphi.
- **Microsoft**, the company that developed and maintains the C# language and the .NET Framework.
-
- **Why**:
- To create a safe, modern language that could address the challenges of developing applications on Windows, while being part of the .NET ecosystem.
- To combine the power of low-level languages with the simplicity and safety of modern programming paradigms like object-oriented programming.
- To provide a language that would be suited for building Windows applications, web services, and enterprise-level systems while being cross-platform with the introduction of .NET Core.
-
introduction
Advantages:
- Object-Oriented: Supports inheritance, polymorphism, and encapsulation, ideal for large applications.
- Rich Ecosystem: Access to the .NET framework, offering libraries for GUI, web, and database development.
- Cross-Platform: With .NET Core, C# works on Windows, Linux, macOS.
- Garbage Collection: Automatic memory management reduces the risk of memory leaks.
- Integration with Microsoft: Works seamlessly with Azure, SQL Server, and other Microsoft technologies.
Disadvantages:
- Memory Heavy: May use more memory compared to low-level languages like C.
- Performance: Slower than languages like C/C++ due to its managed nature.
- Learning Curve: Requires understanding of concepts like asynchronous programming and LINQ.
- Dependency on .NET: Still tied to the .NET ecosystem, which might limit flexibility.
Remember Points:
- Asynchronous Programming: C# supports asynchronous programming with async and await keywords.
- Modern Language Features: Includes LINQ, nullable types, and pattern matching, improving productivity.
Notes
Main()is the entry point of a C# program.Console.WriteLine("Hello World");is used for printing output.
Variables
int intNum = 9;
long longNum = 9999999;
float floatNum = 9.99F;
double doubleNum = 99.999;
decimal decimalNum = 99.9999M;
char letter = 'D';
bool @bool = true;
string site = "quickref.me";
var num = 999;
var str = "999";
var bo = false;C# Strings
String concatenation
string first = "John";
string last = "Doe";
string name = first + " " + last;
Console.WriteLine(name); // => John DoeString interpolation
string first = "John";
string last = "Doe";
string name = $"{first} {last}";
Console.WriteLine(name); // => John DoeVerbatim strings
string longString = @"I can type any characters in here !#@$%^&*()__+ '' \n \t except double quotes and I will be taken literally. I even work with multiple lines.";Member Example
// Using property of System.String
string lengthOfString = "How long?";
lengthOfString.Length // => 9
// Using methods of System.String
lengthOfString.Contains("How"); // => trueString Members
Member Description
---------------------------------------------------
Length A property that returns the length of the string.
Compare() A static method that compares two strings.
Contains() Determines if the string contains a specific substring.
Equals() Determines if the two strings have the same character data.
Format() Formats a string via the {0} notation and by using other primitives.
Trim() Removes all instances of specific characters from trailing and leading characters. Defaults to removing leading and trailing spaces.
Split() Removes the provided character and creates an array out of the remaining characters on either side.Primitive Data Types
DataType Size Range
int 4 bytes -231 to 231-1
long 8 bytes -263 to 263-1
float 4 bytes 6 to 7 decimal digits
double 8 bytes 15 decimal digits
decimal 16 bytes 28 to 29 decimal digits
char 2 bytes 0 to 65535
bool 1 bit true / false
string 2 bytes per char N/AUser Input
Console.WriteLine("Enter number:");
if(int.TryParse(Console.ReadLine(),out int input))
{
// Input validated
Console.WriteLine($"You entered {input}");
}Comments
// Single-line comment
/* Multi-line
comment */
// TODO: Adds comment to a task list in Visual Studio
/// Single-line comment used for documentation
/** Multi-line comment
used for documentation **/Conditionals
int j = 10;
if (j == 10) {
Console.WriteLine("I get printed");
} else if (j > 10) {
Console.WriteLine("I don't");
} else {
Console.WriteLine("I also don't");
}Arrays
char[] chars = new char[10];
chars[0] = 'a';
chars[1] = 'b';
string[] letters = {"A", "B", "C"};
int[] mylist = {100, 200};
bool[] answers = {true, false};Loops
int[] numbers = {1, 2, 3, 4, 5};
for(int i = 0; i < numbers.Length; i++) {
Console.WriteLine(numbers[i]);
}Library & Frameworks
Core Libraries and Frameworks
- .NET 5+ - Base platform for C# applications.
- System.Linq - Language-Integrated Query (LINQ) for collections.
- System.Threading - Multi-threading and asynchronous programming.
- System.Net.Http - HTTP communication and web requests.
GUI Frameworks
- WPF (Windows Presentation Foundation) - Rich desktop applications (Windows only).
- Windows Forms - Simpler framework for Windows desktop applications.
- Blazor - Build interactive web UIs using C# (instead of JavaScript).
Data Management
- Entity Framework Core - ORM for database interaction.
- Dapper - Lightweight ORM for SQL databases.
- MongoDB .NET Driver - MongoDB client driver for .NET.
Networking
- SignalR - Real-time communication library for web apps.
- RestSharp - HTTP client for REST API communication.
Cryptography and Security
- BouncyCastle - Cryptographic APIs for C#.
- IdentityServer - Authentication and authorization framework.
JSON and XML Processing
- Newtonsoft.Json (Json.NET) - JSON serialization/deserialization.
- System.Xml - Built-in XML processing in .NET.
Logging
- Serilog - Flexible logging with support for various sinks.
- NLog - Logging framework with rich configuration options.
Testing and Mocking
Data Compression
- SharpZipLib - Handles ZIP, GZIP, TAR formats.
- System.IO.Compression - Built-in .NET data compression.
Game Development
- Unity3D - Game development engine using C# scripting.
- MonoGame - Cross-platform framework for 2D/3D games.
Miscellaneous
- AutoMapper - Object-to-object mapping library.
- FluentValidation - Fluent interface for .NET validation rules.