Tutorial by Examples: at

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...
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...
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...
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...
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...
Create a new file in your text editor or IDE named HelloWorld.java. Then paste this code block into the file and save: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } } Run live on Ideone Note: For Java to r...
An exception can be caught and handled using the try...catch statement. (In fact try statements take other forms, as described in other examples about try...catch...finally and try-with-resources.) Try-catch with one catch block The most simple form looks like this: try { doSomething(); } ...
Generics enable classes, interfaces, and methods to take other classes and interfaces as type parameters. This example uses generic class Param to take a single type parameter T, delimited by angle brackets (<>): public class Param<T> { private T value; public T getValue() ...
RelativeLayout is a ViewGroup that displays child views in relative positions. By default, all child views are drawn at the top-left of the layout, so you must define the position of each view using the various layout properties available from RelativeLayout.LayoutParams. The value for each layout...
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...
Basic cases int[] numbers1 = new int[3]; // Array for 3 int values, default value is 0 int[] numbers2 = { 1, 2, 3 }; // Array literal of 3 int values int[] numbers3 = new int[] { 1, 2, 3 }; // Array of 3 int values initialized int[][] numbers4 = { { 1, 2...
This example illustrates sending a String with value as "Some data!" from OriginActivity to DestinationActivity. NOTE: This is the most straightforward way of sending data between two activities. See the example on using the starter pattern for a more robust implementation. OriginActivit...
Create the JSONObject using the empty constructor and add fields using the put() method, which is overloaded so that it can be used with different types: try { // Create a new instance of a JSONObject final JSONObject object = new JSONObject(); // With put you can add a name/va...
Custom filters can be set and save from the UI. In the AndroidMonitor tab, click on the right dropdown (must contains Show only selected application or No filters) and select Edit filter configuration. Enter the filter you want And use it (you can selected it from the same dropdown) Important...
Two methods in java.util.Collection create an array from a collection: Object[] toArray() <T> T[] toArray(T[] a) Object[] toArray() can be used as follows: Java SE 5 Set<String> set = new HashSet<String>(); set.add("red"); set.add("blue"); ...
Strings can be internationalised by defining a different strings.xml for each language you support. You add a new language by creating a new values directory with the ISO language code as a suffix. For example, when adding a German set your structure might look like follows: When the system look...

Page 4 of 442