Open Visual Studio
In the toolbar, go to File → New Project
Select the Console Application project type
Open the file Program.cs in the Solution Explorer
Add the following code to Main():
public class Program
{
public static void Main()
{
// Prints a message to the conso...
using System;
class Program
{
// The Main() function is the first function to be executed in a program
static void Main()
{
// Write the string "Hello World to the standard out
Console.WriteLine("Hello World");
}
}
Console.WriteLine has se...
Extension methods were introduced in C# 3.0. Extension methods extend and add behavior to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. They are especially helpful when you cannot modify the source of a type you are looking to enhance. Ex...
It is possible to use await expression to apply await operator to Tasks or Task(Of TResult) in the catch and finally blocks in C#6.
It was not possible to use the await expression in the catch and finally blocks in earlier versions due to compiler limitations. C#6 makes awaiting async tasks a lot e...
HttpClient is available through NuGet: Microsoft HTTP Client Libraries.
string requestUri = "http://www.example.com";
string requestBodyString = "Request body string.";
string contentType = "text/plain";
string requestMethod = "POST";
var request = new ...
This method returns an IEnumerable with all the elements that meets the lambda expression
Example
var personNames = new[]
{
"Foo", "Bar", "Fizz", "Buzz"
};
var namesStartingWithF = personNames.Where(p => p.StartsWith("F"));
Console.W...
try, catch, finally, and throw allow you to handle exceptions in your code.
var processor = new InputProcessor();
// The code within the try block will be executed. If an exception occurs during execution of
// this code, execution will pass to the catch block corresponding to the exception typ...
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
class TcpChat
{
static void Main(string[] args)
{
if(args.Length == 0)
{
Console.WriteLine("Basic TCP chat");
Console.WriteLine();
...
Assemblies are the building block of any Common Language Runtime (CLR) application.
Every type you define, together with its methods, properties and their bytecode, is compiled and packaged inside an Assembly.
using System.Reflection;
Assembly assembly = this.GetType().Assembly;
Assembl...
// Translates to `dict.Add(1, "First")` etc.
var dict = new Dictionary<int, string>()
{
{ 1, "First" },
{ 2, "Second" },
{ 3, "Third" }
};
// Translates to `dict[1] = "First"` etc.
// Works in C# 6.0.
var dict = new Dicti...
using System;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
class HttpGet
{
private static async Task DownloadAsync(string fromUrl, string toFile)
{
using (var fileStream = File.OpenWrite(toFile))
{
using (va...
When you want to catch an exception and do something, but you can't continue execution of the current block of code because of the exception, you may want to rethrow the exception to the next exception handler in the call stack. There are good ways and bad ways to do this.
private static void AskTh...
public class Model
{
public string Name { get; set; }
public bool? Selected { get; set; }
}
Here we have a Class with no constructor with two properties: Name and a nullable boolean property Selected. If we wanted to initialize a List<Model>, there are a few different ways to ex...
using st = System.Text;
//allows you to access classes within this namespace such as StringBuilder
//prefixing them with only the defined alias and not the full namespace. i.e:
//...
var sb = new st.StringBuilder();
//instead of
var sb = new System.Text.StringBuilder();