Given following nullable int
int? i = 10;
In case default value is needed, you can assign one using null coalescing operator, GetValueOrDefault method or check if nullable int HasValue before assignment.
int j = i ?? 0;
int j = i.GetValueOrDefault(0);
int j = i.HasValue ? i.Value : 0;
The ...
Declaring an Event
You can declare an event on any class or struct using the following syntax:
public class MyClass
{
// Declares the event for MyClass
public event EventHandler MyEvent;
// Raises the MyEvent event
public void RaiseEvent()
{
OnMyEvent();
}...
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...
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...
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)
...
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...
public class MyActivity extends Activity {
private static final String PREFS_FILE = "NameOfYourPrefrenceFile";
// PREFS_MODE defines which apps can access the file
private static final int PREFS_MODE = Context.MODE_PRIVATE;
// you can use live template "key"...
private visibility allows a variable to only be accessed by its class. They are often used in conjunction with public getters and setters.
class SomeClass {
private int variable;
public int getVariable() {
return variable;
}
public void setVariable(int variable) {
...
With no modifier, the default is package visibility. From the Java Documentation, "[package visibility] indicates whether classes in the same package as the class (regardless of their parentage) have access to the member." In this example from javax.swing,
package javax.swing;
public abs...
If your computation produces some return value which later is required, a simple Runnable task isn't sufficient. For such cases you can use ExecutorService.submit(Callable<T>) which returns a value after execution completes.
The Service will return a Future which you can use to retrieve the r...
Variables can be accessed via dynamic variable names. The name of a variable can be stored in another variable, allowing it to be accessed dynamically. Such variables are known as variable variables.
To turn a variable into a variable variable, you put an extra $ put in front of your variable.
$va...
The use construct is used to import variables into the anonymous function's scope:
$divisor = 2332;
$myfunction = function($number) use ($divisor) {
return $number / $divisor;
};
echo $myfunction(81620); //Outputs 35
Variables can also be imported by reference:
$collection = [];
$a...
A single case in a switch statement can match on multiple values.
let number = 3
switch number {
case 1, 2:
print("One or Two!")
case 3:
print("Three!")
case 4, 5, 6:
print("Four, Five or Six!")
default:
print("Not One, Two, Three, Four, F...
There are several special variable types that a class can use for more easily sharing data.
Instance variables, preceded by @. They are useful if you want to use the same variable in different methods.
class Person
def initialize(name, age)
my_age = age # local variable, will be destroyed ...
We have three methods:
attr_reader: used to allow reading the variable outside the class.
attr_writer: used to allow modifying the variable outside the class.
attr_accessor: combines both methods.
class Cat
attr_reader :age # you can read the age but you can never change it
attr_writer...