Tutorial by Examples: ce

Simple usage Dapper fully supports stored procs: var user = conn.Query<User>("spGetUser", new { Id = 1 }, commandType: CommandType.StoredProcedure) .SingleOrDefault(); Input, Output and Return parameters If you want something more fancy...
Exception filters give developers the ability to add a condition (in the form of a boolean expression) to a catch block, allowing the catch to execute only if the condition evaluates to true. Exception filters allow the propagation of debug information in the original exception, where as using an...
Declaration: interface IMyGenericInterface<T1, T2, T3, ...> { ... } Usage (in inheritance): class ClassA<T1, T2, T3> : IMyGenericInterface<T1, T2, T3> { ... } class ClassB<T1, T2> : IMyGenericInterface<T1, T2, int> { ... } class ClassC<T1> : IMyGenericI...
An interface is used to enforce the presence of a method in any class that 'implements' it. The interface is defined with the keyword interface and a class can 'implement' it by adding : InterfaceName after the class name. A class can implement multiple interfaces by separating each interface with ...
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...
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...
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...
var numbers = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; var evenNumbersBetweenSixAndFourteen = new[] { 6, 8, 10, 12 }; var result = numbers.Except(evenNumbersBetweenSixAndFourteen); Console.WriteLine(string.Join(",", result)); //1, 2, 3, 4, 5, 7, 9
var numbers = new[] {1,2,3,4,5}; var sameNumbers = new[] {1,2,3,4,5}; var sameNumbersInDifferentOrder = new[] {5,1,4,2,3}; var equalIfSameOrder = numbers.SequenceEqual(sameNumbers); Console.WriteLine(equalIfSameOrder); //True var equalIfDifferentOrder = numbers.SequenceEqual(sameNumbersInDi...
The namespace keyword is an organization construct that helps us understand how a codebase is arranged. Namespaces in C# are virtual spaces rather than being in a physical folder. namespace StackOverflow { namespace Documentation { namespace CSharp.Keywords { ...
public interface IAnimal { string Name { get; set; } } public interface INoiseMaker { string MakeNoise(); } public class Cat : IAnimal, INoiseMaker { public Cat() { Name = "Cat"; } public string Name { get; set; } public string M...
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...
interface BaseInterface {} class BaseClass : BaseInterface {} interface DerivedInterface {} class DerivedClass : BaseClass, DerivedInterface {} var baseInterfaceType = typeof(BaseInterface); var derivedInterfaceType = typeof(DerivedInterface); var baseType = typeof(BaseClass); var derived...
string sqrt = "\u221A"; // √ string emoji = "\U0001F601"; // 😁 string text = "\u0022Hello World\u0022"; // "Hello World" string variableWidth = "\x22Hello World\x22"; // "Hello World"
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...
// 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...
string[] strings = new[] {"foo", "bar"}; object[] objects = strings; // implicit conversion from string[] to object[] This conversion is not type-safe. The following code will raise a runtime exception: string[] strings = new[] {"Foo"}; object[] objects = strings;...
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...

Page 1 of 134