Tutorial by Examples: d

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))
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...
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...
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...
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) ...
Snippet public class SomeClass<TItem> { public void PrintTypeName() { Console.WriteLine(nameof(TItem)); } } ... var myClass = new SomeClass<int>(); myClass.PrintTypeName(); Console.WriteLine(nameof(SomeClass<int>)); Console Output TItem S...
Snippet Console.WriteLine(nameof(CompanyNamespace.MyNamespace)); Console.WriteLine(nameof(MyClass)); Console.WriteLine(nameof(MyClass.MyNestedClass)); Console.WriteLine(nameof(MyNamespace.MyClass.MyNestedClass.MyStaticProperty)); Console Output MyNamespace MyClass MyNestedClass MyStatic...
Create a property with getter and/or setter and initialize all in one line: public string Foobar { get; set; } = "xyz";
public string Foobar { get { return _foobar; } set { _foobar = value; } } private string _foobar = "xyz";
Declaration of an interface using the interface keyword: public interface Animal { String getSound(); // Interface methods are public by default } Override Annotation @Override public String getSound() { // Code goes here... } This forces the compiler to check that we are overri...
We can create an ArrayList (following the List interface): List aListOfFruits = new ArrayList(); Java SE 5 List<String> aListOfFruits = new ArrayList<String>(); Java SE 7 List<String> aListOfFruits = new ArrayList<>(); Now, use the method add to add a String: ...
Sorting lists Prior to Java 8, it was necessary to implement the java.util.Comparator interface with an anonymous (or named) class when sorting a list1: Java SE 1.2 List<Person> people = ... Collections.sort( people, new Comparator<Person>() { public int compare(Pe...
This is an example of a default build.gradle file in a module. apply plugin: 'com.android.application' android { compileSdkVersion 25 buildToolsVersion '25.0.3' signingConfigs { applicationName { keyAlias 'applicationName' keyPassword 'password...
To use a NavigationView just add the dependency in the build.gradle file as described in the remarks section Then add the NavigationView in the layout <?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout xmlns:android="http://schema...

Page 7 of 691