Tutorial by Examples: ci

Extension methods can also be used like ordinary static class methods. This way of calling an extension method is more verbose, but is necessary in some cases. static class StringExtensions { public static string Shorten(this string text, int length) { return text.Substring(0, ...
By definition, the short-circuiting boolean operators will only evaluate the second operand if the first operand can not determine the overall result of the expression. It means that, if you are using && operator as firstCondition && secondCondition it will evaluate secondCondition ...
Apostrophes char apostrophe = '\''; Backslash char oneBackslash = '\\';
Backslash // The filename will be c:\myfile.txt in both cases string filename = "c:\\myfile.txt"; string filename = @"c:\myfile.txt"; The second example uses a verbatim string literal, which doesn't treat the backslash as an escape character. Quotes string text = "\&...
try { /* code to open a file */ } catch (System.IO.FileNotFoundException) { /* code to handle the file being not found */ } catch (System.IO.UnauthorizedAccessException) { /* code to handle not being allowed access to the file */ } catch (System.IO.IOException) { /* cod...
using st = System.Text; //allows you to access classes within this namespace such as StringBuilder //prefixing them with only the defined alias and not the full namespace. i.e: //... var sb = new st.StringBuilder(); //instead of var sb = new System.Text.StringBuilder();
A task can be created by directly instantiating the Task class... var task = new Task(() => { Console.WriteLine("Task code starting..."); Thread.Sleep(2000); Console.WriteLine("...task code ending!"); }); Console.WriteLine("Starting task..."); t...
When passing formal arguments to a generic method, relevant generic type arguments can usually be inferred implicitly. If all generic type can be inferred, then specifying them in the syntax is optional. Consider the following generic method. It has one formal parameter and one generic type paramet...
Explicit interface implementation is necessary when you implement multiple interfaces who define a common method, but different implementations are required depending on which interface is being used to call the method (note that you don't need explicit implementations if multiple interfaces share t...
string helloWorld = "hello world, how is it going?"; string[] parts1 = helloWorld.Split(','); //parts1: ["hello world", " how is it going?"] string[] parts2 = helloWorld.Split(' '); //parts2: ["hello", "world,", "how", "is&qu...
Dependencies can be added for a specific product flavor, similar to how they can be added for specific build configurations. For this example, assume that we have already defined two product flavors called free and paid (more on defining flavors here). We can then add the AdMob dependency for the ...
Resources can be added for a specific product flavor. For this example, assume that we have already defined two product flavors called free and paid. In order to add product flavor-specific resources, we create additional resource folders alongside the main/res folder, which we can then add resourc...
Data model public class Item { private String name; public String getName() { return name; } } Layout XML You must import referenced classes, just as you would in Java. <?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="h...
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...
Enum cases can contain one or more payloads (associated values): enum Action { case jump case kick case move(distance: Float) // The "move" case has an associated distance } The payload must be provided when instantiating the enum value: performAction(.jump) performA...
Logical OR (||), reading left to right, will evaluate to the first truthy value. If no truthy value is found, the last value is returned. var a = 'hello' || ''; // a = 'hello' var b = '' || []; // b = [] var c = '' || undefined; // c = undefined var d = 1 |...
Protocols may define associated type requirements using the associatedtype keyword: protocol Container { associatedtype Element var count: Int { get } subscript(index: Int) -> Element { get set } } Protocols with associated type requirements can only be used as generic constraints:...
Setting a specific Seed will create a fixed random-number series: random.seed(5) # Create a fixed state print(random.randrange(0, 10)) # Get a random integer between 0 and 9 # Out: 9 print(random.randrange(0, 10)) # Out: 4 Resetting the seed will create the same &qu...
Instead of importing the complete module you can import only specified names: from random import randint # Syntax "from MODULENAME import NAME1[, NAME2[, ...]]" print(randint(1, 10)) # Out: 5 from random is needed, because the python interpreter has to know from which resource it...
Getting the minimum or maximum or using sorted depends on iterations over the object. In the case of dict, the iteration is only over the keys: adict = {'a': 3, 'b': 5, 'c': 1} min(adict) # Output: 'a' max(adict) # Output: 'c' sorted(adict) # Output: ['a', 'b', 'c'] To keep the dictionary ...

Page 1 of 42