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...
string fullOrRelativePath = "testfile.txt";
string fileData;
using (var reader = new StreamReader(fullOrRelativePath))
{
fileData = reader.ReadToEnd();
}
Note that this StreamReader constructor overload does some auto encoding detection, which may or may not conform to the ...
Expression-bodied function members allow the use of lambda expressions as member bodies. For simple members, it can result in cleaner and more readable code.
Expression-bodied functions can be used for properties, indexers, methods, and operators.
Properties
public decimal TotalPrice => Base...
Get Instance method and invoke it
using System;
public class Program
{
public static void Main()
{
var theString = "hello";
var method = theString
.GetType()
.GetMethod("Substring",
...
public class Animal
{
public string Name { get; set; }
}
public interface INoiseMaker
{
string MakeNoise();
}
//Note that in C#, the base class name must come before the interface names
public class Cat : Animal, INoiseMaker
{
public Cat()
{
Name = "Ca...
HttpClient is available through NuGet: Microsoft HTTP Client Libraries.
string requestUri = "http://www.example.com";
string responseData;
using (var client = new HttpClient())
{
using(var response = client.GetAsync(requestUri).Result)
{
response.EnsureSuccessStatus...
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 ...
var persons = new[]
{
new {Id = 1, Name = "Foo"},
new {Id = 2, Name = "Bar"},
new {Id = 3, Name = "Fizz"},
new {Id = 4, Name = "Buzz"}
};
var personsSortedByNameDescending = persons.OrderByDescending(p => p.Name);
Console.WriteL...
Returns a new dictionary from the source IEnumerable using the provided keySelector function to determine keys. Will throw an ArgumentException if keySelector is not injective(returns a unique value for each member of the source collection.) There are overloads which allow one to specify the value t...
public class LivingBeing
{
string Name { get; set; }
}
public interface IAnimal
{
bool HasHair { get; set; }
}
public interface INoiseMaker
{
string MakeNoise();
}
//Note that in C#, the base class name must come before the interface names
public class Cat : LivingBei...
Overloading just equality operators is not enough. Under different circumstances, all of the following can be called:
object.Equals and object.GetHashCode
IEquatable<T>.Equals (optional, allows avoiding boxing)
operator == and operator != (optional, allows using operators)
When overrid...
The ?. operator is syntactic sugar to avoid verbose null checks. It's also known as the Safe navigation operator.
Class used in the following example:
public class Person
{
public int Age { get; set; }
public string Name { get; set; }
public Person Spouse { get; set; }
}
If a...
Similarly to the ?. operator, the null-conditional index operator checks for null values when indexing into a collection that may be null.
string item = collection?[index];
is syntactic sugar for
string item = null;
if(collection != null)
{
item = collection[index];
}
You can enumerate through a Dictionary in one of 3 ways:
Using KeyValue pairs
Dictionary<int, string> dict = new Dictionary<int, string>();
foreach(KeyValuePair<int, string> kvp in dict)
{
Console.WriteLine("Key : " + kvp.Key.ToString() + ", Value : " +...