Tutorial by Examples: am

You can use default parameters if you want to provide the option to leave out parameters: static void SaySomething(string what = "ehh") { Console.WriteLine(what); } static void Main() { // prints "hello" SaySomething("hello"); // prints &quot...
Typically lambdas are used for defining simple functions (generally in the context of a linq expression): var incremented = myEnumerable.Select(x => x + 1); Here the return is implicit. However, it is also possible to pass actions as lambdas: myObservable.Do(x => Console.WriteLine(x)); ...
Use parentheses around the expression to the left of the => operator to indicate multiple parameters. delegate int ModifyInt(int input1, int input2); ModifyInt multiplyTwoInts = (x,y) => x * y; Similarly, an empty set of parentheses indicates that the function does not accept parameters. ...
Unlike an expression lambda, a statement lambda can contain multiple statements separated by semicolons. delegate void ModifyInt(int input); ModifyInt addOneAndTellMe = x => { int result = x + 1; Console.WriteLine(result); }; Note that the statements are enclosed in braces {}. ...
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...
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...
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...
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...
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 void DoSomething(int paramValue) { Console.WriteLine(nameof(paramValue)); } ... int myValue = 10; DoSomething(myValue); Console Output paramValue
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...
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...
A Stream is a sequence of elements upon which sequential and parallel aggregate operations can be performed. Any given Stream can potentially have an unlimited amount of data flowing through it. As a result, data received from a Stream is processed individually as it arrives, as opposed to performin...
Collect with toList() and toSet() Elements from a Stream can be easily collected into a container by using the Stream.collect operation: System.out.println(Arrays .asList("apple", "banana", "pear", "kiwi", "orange") .stream() .filter...
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...
public class SampleAdapter extends RecyclerView.Adapter<SampleAdapter.ViewHolder> { private String[] mDataSet; private OnRVItemClickListener mListener; /** * Provide a reference to the type of views that you are using (custom ViewHolder) */ public static cla...
First example reimplemented in Kotlin and using RxJava for cleaner interaction. import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.support.v7.widget.RecyclerView import rx.subjects.PublishSubject public class SampleAdapter(private val ite...
When we create any View in layout, we can use the android:onClick attribute to reference a method in the associated activity or fragment to handle the click events. XML Layout <Button android:id="@+id/button" ... // onClick should reference the method in your activity or fra...
Java SE 7 Java 7 introduced the Diamond1 to remove some boiler-plate around generic class instantiation. With Java 7+ you can write: List<String> list = new LinkedList<>(); Where you had to write in previous versions, this: List<String> list = new LinkedList<String>()...

Page 2 of 129