Tutorial by Examples: a

string helloWorld = "hello world, how is it going?"; string[] parts1 = helloWorld.Split(','); //parts1: ["hello world", " how is it going?"] string[] parts2 = helloWorld.Split(' '); //parts2: ["hello", "world,", "how", "is&qu...
string helloWorld = "Hello World!"; string world = helloWorld.Substring(6); //world = "World!" string hello = helloWorld.Substring(0,5); // hello = "Hello" Substring returns the string up from a given index, or between two indexes (both inclusive).
string HelloWorld = "Hello World"; HelloWorld.StartsWith("Hello"); // true HelloWorld.StartsWith("Foo"); // false Finding a string within a string Using the System.String.Contains you can find out if a particular string exists within a string. The method returns ...
String.Trim() string x = " Hello World! "; string y = x.Trim(); // "Hello World!" string q = "{(Hi!*"; string r = q.Trim( '(', '*', '{' ); // "Hi!" String.TrimStart() and String.TrimEnd() string q = "{(Hi*"; string r = q.TrimStart( '{...
Unlike interfaces, which can be described as contracts for implementation, abstract classes act as contracts for extension. An abstract class cannot be instantiated, it must be extended and the resulting class (or derived class) can then be instantiated. Abstract classes are used to provide generi...
Use the String.Format() method to replace one or more items in the string with the string representation of a specified object: String.Format("Hello {0} Foo {1}", "World", "Bar") //Hello World Foo Bar
The Range and Repeat static methods on Enumerable can be used to generate simple sequences. Range Enumerable.Range() generates a sequence of integers given a starting value and a count. // Generate a collection containing the numbers 1-100 ([1, 2, 3, ..., 98, 99, 100]) var range = Enumerable.Ran...
The following is a bad idea because it would dispose the db variable before returning it. public IDBContext GetDBContext() { using (var db = new DBContext()) { return db; } } This can also create more subtle mistakes: public IEnumerable<Person> GetPeople(int age)...
The Skip method returns a collection excluding a number of items from the beginning of the source collection. The number of items excluded is the number given as an argument. If there are less items in the collection than specified in the argument then an empty collection is returned. The Take meth...
All six methods return a single value of the sequence type, and can be called with or without a predicate. Depending on the number of elements that match the predicate or, if no predicate is supplied, the number of elements in the source sequence, they behave as follows: First() Returns the fir...
var color = "Black"; var age = 4; var query = "Select * from Cats where Color = :Color and Age > :Age"; var dynamicParameters = new DynamicParameters(); dynamicParameters.Add("Color", color); dynamicParameters.Add("Age", age); using (var connectio...
Event declaration: public event EventHandler<EventArgsT> EventName; Event handler declaration: public void HandlerName(object sender, EventArgsT args) { /* Handler logic */ } Subscribing to the event: Dynamically: EventName += HandlerName; Through the Designer: Click the Events...
Event declaration: public event EventHandler<EventArgsType> EventName; Event handler declaration using lambda operator => and subscribing to the event: EventName += (obj, eventArgs) => { /* Handler logic */ }; Event handler declaration using delegate anonymous method syntax: Eve...
Events can be of any delegate type, not just EventHandler and EventHandler<T>. For example: //Declaring an event public event Action<Param1Type, Param2Type, ...> EventName; This is used similarly to standard EventHandler events: //Adding a named event handler public void HandlerNa...
Given an async method like this: Task<string> GetNameAsync(CancellationToken cancellationToken) Wrap it as an IObservable<string> like this: Observable.FromAsync(cancellationToken => GetNameAsync(cancellationToken))
Given an IObservable<Offer> of offers from merchants to buy or sell some type of item at a fixed price, we can match pairs of buyers and sellers as follows: var sellers = offers.Where(offer => offer.IsSell).Select(offer => offer.Merchant); var buyers = offers.Where(offer => offer.Is...
using System; using System.Reflection; using System.Reflection.Emit; class DemoAssemblyBuilder { public static void Main() { // An assembly consists of one or more modules, each of which // contains zero or more types. This code creates a single-module // a...
using System.Linq.Expressions; // Manually build the expression tree for // the lambda expression num => num < 5. ParameterExpression numParam = Expression.Parameter(typeof(int), "num"); ConstantExpression five = Expression.Constant(5, typeof(int)); BinaryExpression numLessTh...
using System.Linq.Expressions; // Create an expression tree. Expression<Func<int, bool>> exprTree = num => num < 5; // Decompose the expression tree. ParameterExpression param = (ParameterExpression)exprTree.Parameters[0]; BinaryExpression operation = (BinaryExpression)exp...
Set function inserts a cache entry into the cache by using a CacheItem instance to supply the key and value for the cache entry. This function Overrides ObjectCache.Set(CacheItem, CacheItemPolicy) private static bool SetToCache() { string key = "Cache_Key"; string value = &quo...

Page 11 of 1099