Tutorial by Examples: ch

public class IHtmlStringTypeHandler : SqlMapper.TypeHandler<IHtmlString> { public override void SetValue( IDbDataParameter parameter, IHtmlString value) { parameter.DbType = DbType.String; parameter.Value = value?.ToHtmlString(); } pu...
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...
Code can and should throw exceptions in exceptional circumstances. Examples of this include: Attempting to read past the end of a stream Not having necessary permissions to access a file Attempting to perform an invalid operation, such as dividing by zero A timeout occurring when downloading a...
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(); ...
Extension methods are static methods which behave like instance methods. However, unlike what happens when calling an instance method on a null reference, when an extension method is called with a null reference, it does not throw a NullReferenceException. This can be quite useful in some scenarios....
string sqrt = "\u221A"; // √ string emoji = "\U0001F601"; // 😁 string text = "\u0022Hello World\u0022"; // "Hello World" string variableWidth = "\x22Hello World\x22"; // "Hello World"
Apostrophes char apostrophe = '\''; Backslash char oneBackslash = '\\';
// 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';
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...
The checked and unchecked keywords define how operations handle mathematical overflow. "Overflow" in the context of the checked and unchecked keywords is when an integer arithmetic operation results in a value which is greater in magnitude than the target data type can represent. When ove...
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...
6.0 As of C# 6.0, the await keyword can now be used within a catch and finally block. try { var client = new AsyncClient(); await client.DoSomething(); } catch (MyException ex) { await client.LogExceptionAsync(); throw; } finally { await client.CloseAsync(); } 5.06.0 P...
An iterator method is not executed until the return value is enumerated. It's therefore advantageous to assert preconditions outside of the iterator. public static IEnumerable<int> Count(int start, int count) { // The exception will throw when the method is called, not when the result i...
int? i = null; if (i != null) { Console.WriteLine("i is not null"); } else { Console.WriteLine("i is null"); } Which is the same as: if (i.HasValue) { Console.WriteLine("i is not null"); } else { Console.WriteLine("i is null&quot...
The left-hand operand must be nullable, while the right-hand operand may or may not be. The result will be typed accordingly. Non-nullable int? a = null; int b = 3; var output = a ?? b; var type = output.GetType(); Console.WriteLine($"Output Type :{type}"); Console.WriteLine($&q...
Many LINQ functions both operate on an IEnumerable<TSource> and also return an IEnumerable<TResult>. The type parameters TSource and TResult may or may not refer to the same type, depending on the method in question and any functions passed to it. A few examples of this are public stat...
using System.Speech.Recognition; // ... SpeechRecognitionEngine recognitionEngine = new SpeechRecognitionEngine(); recognitionEngine.LoadGrammar(new DictationGrammar()); recognitionEngine.SpeechRecognized += delegate(object sender, SpeechRecognizedEventArgs e) { Console.WriteLine(&quot...
SpeechRecognitionEngine recognitionEngine = new SpeechRecognitionEngine(); GrammarBuilder builder = new GrammarBuilder(); builder.Append(new Choices("I am", "You are", "He is", "She is", "We are", "They are")); builder.Append(new Choices...

Page 1 of 109