Tutorial by Examples

The following examples will not compile: string s = "\c"; char c = '\c'; Instead, they will produce the error Unrecognized escape sequence at compile time.
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...
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 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) ...
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...
Snippet Console.WriteLine(nameof(CompanyNamespace.MyNamespace)); Console.WriteLine(nameof(MyClass)); Console.WriteLine(nameof(MyClass.MyNestedClass)); Console.WriteLine(nameof(MyNamespace.MyClass.MyNestedClass.MyStaticProperty)); Console Output MyNamespace MyClass MyNestedClass MyStatic...
Create a property with getter and/or setter and initialize all in one line: public string Foobar { get; set; } = "xyz";
public string Foobar { get { return _foobar; } set { _foobar = value; } } private string _foobar = "xyz";
class Example { public string Foobar { get; set; } public List<string> Names { get; set; } public Example() { Foobar = "xyz"; Names = new List<string>(){"carrot","fox","ball"}; } }
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...
Say we have a button (we can create it programmatically, or bind it from a view using findViewbyId(), etc...) Button btnOK = (...) Now, create an anonymous class and set it inline. btnOk.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ...
Declaration of an interface using the interface keyword: public interface Animal { String getSound(); // Interface methods are public by default } Override Annotation @Override public String getSound() { // Code goes here... } This forces the compiler to check that we are overri...
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...
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(); } ...
We can create an ArrayList (following the List interface): List aListOfFruits = new ArrayList(); Java SE 5 List<String> aListOfFruits = new ArrayList<String>(); Java SE 7 List<String> aListOfFruits = new ArrayList<>(); Now, use the method add to add a String: ...
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...

Page 15 of 1336