Tutorial by Examples

Backslash // The filename will be c:\myfile.txt in both cases string filename = "c:\\myfile.txt"; string filename = @"c:\myfile.txt"; The second example uses a verbatim string literal, which doesn't treat the backslash as an escape character. Quotes string text = "\&...
try { /* code that could throw an exception */ } catch (Exception ex) { /* handle the exception */ } Note that handling all exceptions with the same code is often not the best approach. This is commonly used when any inner exception handling routines fail, as a last resort.
try { /* code to open a file */ } catch (System.IO.FileNotFoundException) { /* code to handle the file being not found */ } catch (System.IO.UnauthorizedAccessException) { /* code to handle not being allowed access to the file */ } catch (System.IO.IOException) { /* cod...
You are allowed to create and throw exceptions in your own code. Instantiating an exception is done the same way that any other C# object. Exception ex = new Exception(); // constructor with an overload that takes a message string Exception ex = new Exception("Error message"); Yo...
try { /* code that could throw an exception */ } catch (Exception) { /* handle the exception */ } finally { /* Code that will be executed, regardless if an exception was thrown / caught or not */ } The try / catch / finally block can be very handy when reading from files. ...
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]; }
// assign string from a string literal string s = "hello"; // assign string from an array of characters char[] chars = new char[] { 'h', 'e', 'l', 'l', 'o' }; string s = new string(chars, 0, chars.Length); // assign string from a char pointer, derived from a string string s; uns...
// single character s char c = 's'; // character s: casted from integer value char c = (char)115; // unicode character: single character s char c = '\u0073'; // unicode character: smiley face char c = '\u263a';
// assigning a signed short to its minimum value short s = -32768; // assigning a signed short to its maximum value short s = 32767; // assigning a signed int to its minimum value int i = -2147483648; // assigning a signed int to its maximum value int i = 2147483647; // assigning a s...
// assigning an unsigned short to its minimum value ushort s = 0; // assigning an unsigned short to its maximum value ushort s = 65535; // assigning an unsigned int to its minimum value uint i = 0; // assigning an unsigned int to its maximum value uint i = 4294967295; // assigning an...
In order to be able to manage your projects' packages, you need the NuGet Package Manager. This is a Visual Studio Extension, explained in the official docs: Installing and Updating NuGet Client. Starting with Visual Studio 2012, NuGet is included in every edition, and can be used from: Tools ->...
When you right-click a project (or its References folder), you can click the "Manage NuGet Packages..." option. This shows the Package Manager Dialog.
Click the menus Tools -> NuGet Package Manager -> Package Manager Console to show the console in your IDE. Official documentation here. Here you can issue, amongst others, install-package commands which installs the entered package into the currently selected "Default project": Ins...
public class SomeClass { public void DoStuff() { } protected void DoMagic() { } } public static class SomeClassExtensions { public static void DoStuffWrapper(this SomeClass someInstance) { someInstance.DoStuff(); // ok ...
The ref and out keywords cause an argument to be passed by reference, not by value. For value types, this means that the value of the variable can be changed by the callee. int x = 5; ChangeX(ref x); // The value of x could be different now For reference types, the instance in the variable can...
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...
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 : " +...
// 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...
Dictionary<int, string> dict = new Dictionary<int, string>(); dict.Add(1, "First"); dict.Add(2, "Second"); // To safely add items (check to ensure item does not already exist - would throw) if(!dict.ContainsKey(3)) { dict.Add(3, "Third"); } Al...

Page 8 of 1336