Tutorial by Examples: c

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...
// Define an expression tree, taking an integer, returning a bool. Expression<Func<int, bool>> expr = num => num < 5; // Call the Compile method on the expression tree to return a delegate that can be called. Func<int, bool> result = expr.Compile(); // Invoke the dele...
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...
var sequenceOfSequences = new [] { new [] { 1, 2, 3 }, new [] { 4, 5 }, new [] { 6 } }; var sequence = sequenceOfSequences.SelectMany(x => x); // returns { 1, 2, 3, 4, 5, 6 } Use SelectMany() if you have, or you are creating a sequence of sequences, but you want the result as one long sequen...
The SelectMany linq method 'flattens' an IEnumerable<IEnumerable<T>> into an IEnumerable<T>. All of the T elements within the IEnumerable instances contained in the source IEnumerable will be combined into a single IEnumerable. var words = new [] { "a,b,c", "d,e&quo...
This code will subscribe to the emails observable twice: emails.Where(email => email.From == "John").Subscribe(email => Console.WriteLine("A")); emails.Where(email => email.From == "Mary").Subscribe(email => Console.WriteLine("B")); To share a...
This code contains an overloaded method named Hello: class Example { public static void Hello(int arg) { Console.WriteLine("int"); } public static void Hello(double arg) { Console.WriteLine("double"); } public static vo...
The following program: class Program { static void Method(params Object[] objects) { System.Console.WriteLine(objects.Length); } static void Method(Object a, Object b) { System.Console.WriteLine("two"); } static void Main(string[] a...
The following examples will not compile: string s = "\c"; char c = '\c'; Instead, they will produce the error Unrecognized escape sequence at compile time.
There are several places where you can use String.Format indirectly: The secret is to look for the overload with the signature string format, params object[] args, e.g.: Console.WriteLine(String.Format("{0} - {1}", name, value)); Can be replaced with shorter version: Console.WriteLine...
NumberFormatInfo can be used for formatting both integer and float numbers. // invariantResult is "1,234,567.89" var invarianResult = string.Format(CultureInfo.InvariantCulture, "{0:#,###,##}", 1234567.89); // NumberFormatInfo is one of classes that implement IFormatProvider...
The nameof operator allows you to get the name of a variable, type or member in string form without hard-coding it as a literal. The operation is evaluated at compile-time, which means that you can rename, using an IDE's rename feature, a referenced identifier and the name string will update with it...
Snippet public class Person : INotifyPropertyChanged { private string _address; public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged(string propertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName))...
Snippet public class BugReport : INotifyPropertyChanged { public string Title { ... } public BugStatus Status { ... } } ... private void BugReport_PropertyChanged(object sender, PropertyChangedEventArgs e) { var bugReport = (BugReport)sender; switch (e.PropertyName) ...

Page 9 of 826