Tutorial by Examples: au

Introduction Properties can be initialized with the = operator after the closing }. The Coordinate class below shows the available options for initializing a property: 6.0 public class Coordinate { public int X { get; set; } = 34; // get or set auto-property with initializer public ...
When a type is defined without a constructor: public class Animal { } then the compiler generates a default constructor equivalent to the following: public class Animal { public Animal() {} } The definition of any constructor for the type will suppress the default constructor genera...
var numbers = new[] {1,2,3,4,5}; var lastNumber = numbers.LastOrDefault(); Console.WriteLine(lastNumber); //5 var lastEvenNumber = numbers.LastOrDefault(n => (n & 1) == 0); Console.WriteLine(lastEvenNumber); //4 var lastNegativeNumber = numbers.LastOrDefault(n => n < 0); Con...
var oneNumber = new[] {5}; var theOnlyNumber = oneNumber.SingleOrDefault(); Console.WriteLine(theOnlyNumber); //5 var numbers = new[] {1,2,3,4,5}; var theOnlyNumberSmallerThanTwo = numbers.SingleOrDefault(n => n < 2); Console.WriteLine(theOnlyNumberSmallerThanTwo); //1 var theOnl...
var numbers = new[] {1,2,3,4,5}; var firstNumber = numbers.FirstOrDefault(); Console.WriteLine(firstNumber); //1 var firstEvenNumber = numbers.FirstOrDefault(n => (n & 1) == 0); Console.WriteLine(firstEvenNumber); //2 var firstNegativeNumber = numbers.FirstOrDefault(n => n < ...
For classes, interfaces, delegate, array, nullable (such as int?) and pointer types, default(TheType) returns null: class MyClass {} Debug.Assert(default(MyClass) == null); Debug.Assert(default(string) == null); For structs and enums, default(TheType) returns the same as new TheType(): struct...
var names = new[] {"Foo","Bar","Fizz","Buzz"}; var thirdName = names.ElementAtOrDefault(2); Console.WriteLine(thirdName); //Fizz var minusOnethName = names.ElementAtOrDefault(-1); Console.WriteLine(minusOnethName); //null var fifthName = names.Eleme...
var numbers = new[] {2,4,6,8,1,3,5,7}; var numbersOrDefault = numbers.DefaultIfEmpty(); Console.WriteLine(numbers.SequenceEqual(numbersOrDefault)); //True var noNumbers = new int[0]; var noNumbersOrDefault = noNumbers.DefaultIfEmpty(); Console.WriteLine(noNumbersOrDefault.Count()); //1 C...
When you want to catch an exception and do something, but you can't continue execution of the current block of code because of the exception, you may want to rethrow the exception to the next exception handler in the call stack. There are good ways and bad ways to do this. private static void AskTh...
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...
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...
Create a property with getter and/or setter and initialize all in one line: public string Foobar { get; set; } = "xyz";
Examples of using Default Methods introduced in Java 8 in Map interface Using getOrDefault Returns the value mapped to the key, or if the key is not present, returns the default value Map<Integer, String> map = new HashMap<>(); map.put(1, "First element"); map.get(1); ...
/** * Interface with default method */ public interface Printable { default void printString() { System.out.println( "default implementation" ); } } /** * Class which falls back to default implementation of {@link #printString()} */ public class WithDefault...
You can as well access other interface methods from within your default method. public interface Summable { int getA(); int getB(); default int calculateSum() { return getA() + getB(); } } public class Sum implements Summable { @Override public int get...
All Swing-related operations happen on a dedicated thread (the EDT - Event Dispatch Thread). If this thread gets blocked, the UI becomes non-responsive. Therefore, if you want to delay an operation you cannot use Thread.sleep. Use a javax.swing.Timer instead. For example the following Timer will re...
Updating the state of a Swing component must happen on the Event Dispatch Thread (the EDT). The javax.swing.Timer triggers its ActionListener on the EDT, making it a good choice to perform Swing operations. The following example updates the text of a JLabel each two seconds: //Use a timer to updat...
In the ActionListener attached to a javax.swing.Timer, you can keep track of the number of times the Timer executed the ActionListener. Once the required number of times is reached, you can use the Timer#stop() method to stop the Timer. Timer timer = new Timer( delay, new ActionListener() { priv...
Opening with the default browser This example shows how you can open a URL programmatically in the built-in web browser rather than within your application. This allows your app to open up a webpage without the need to include the INTERNET permission in your manifest file. public void onBrowseCli...
Don't just use Optional.get() since that may throw NoSuchElementException. The Optional.orElse(T) and Optional.orElseGet(Supplier<? extends T>) methods provide a way to supply a default value in case the Optional is empty. String value = "something"; return Optional.ofNullable(v...

Page 1 of 37