Tutorial by Examples: ast

For types known at compile-time, use a generic parameter with Query<T>. public class Dog { public int? Age { get; set; } public Guid Id { get; set; } public string Name { get; set; } public float? Weight { get; set; } public int IgnoredProperty { get { return 1; } ...
string requestUri = "http://www.example.com"; string requestBodyString = "Request body string."; string contentType = "text/plain"; string requestMethod = "POST"; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUri) { Method = reque...
string requestUri = "http://www.example.com"; string requestBodyString = "Request body string."; string contentType = "text/plain"; string requestMethod = "POST"; byte[] responseBody; byte[] requestBodyBytes = Encoding.UTF8.GetBytes(requestBodyS...
HttpClient is available through NuGet: Microsoft HTTP Client Libraries. string requestUri = "http://www.example.com"; string requestBodyString = "Request body string."; string contentType = "text/plain"; string requestMethod = "POST"; var request = new ...
var numbers = new[] {1,2,3,4,5}; var lastNumber = numbers.Last(); Console.WriteLine(lastNumber); //5 var lastEvenNumber = numbers.Last(n => (n & 1) == 0); Console.WriteLine(lastEvenNumber); //4 The following throws InvalidOperationException: var lastNegativeNumber = numbers.Last(n...
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...
Cast is different from the other methods of Enumerable in that it is an extension method for IEnumerable, not for IEnumerable<T>. Thus it can be used to convert instances of the former into instances of the later. This does not compile since ArrayList does not implement IEnumerable<T>: ...
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 {}. ...
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...
string HelloWorld = "Hello World"; HelloWorld.StartsWith("Hello"); // true HelloWorld.StartsWith("Foo"); // false Finding a string within a string Using the System.String.Contains you can find out if a particular string exists within a string. The method returns ...
Use the String.Format() method to replace one or more items in the string with the string representation of a specified object: String.Format("Hello {0} Foo {1}", "World", "Bar") //Hello World Foo Bar
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...
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...
Java SE 5 Since Java 1.5 you can get a String representation of the contents of the specified array without iterating over its every element. Just use Arrays.toString(Object[]) or Arrays.deepToString(Object[]) for multidimentional arrays: int[] arr = {1, 2, 3, 4, 5}; System.out.println(Arrays.toS...
Strings can be internationalised by defining a different strings.xml for each language you support. You add a new language by creating a new values directory with the ISO language code as a suffix. For example, when adding a German set your structure might look like follows: When the system look...
The String type provides two methods for converting strings between upper case and lower case: toUpperCase to convert all characters to upper case toLowerCase to convert all characters to lower case These methods both return the converted strings as new String instances: the original String o...
To check whether a particular String a is being contained in a String b or not, we can use the method String.contains() with the following syntax: b.contains(a); // Return true if a is contained in b, false otherwise The String.contains() method can be used to verify if a CharSequence can be fou...
In order to get the length of a String object, call the length() method on it. The length is equal to the number of UTF-16 code units (chars) in the string. String str = "Hello, World!"; System.out.println(str.length()); // Prints out 13 Live Demo on Ideone A char in a String is UTF...
Sometimes you may wish to read byte-input into a String. To do this you will need to find something that converts between byte and the "native Java" UTF-16 Codepoints used as char. That is done with a InputStreamReader. To speed the process up a bit, it's "usual" to allocate a b...
String str = "My String"; System.out.println(str.charAt(0)); // "M" System.out.println(str.charAt(1)); // "y" System.out.println(str.charAt(2)); // " " System.out.println(str.charAt(str.length-1)); // Last character "g" To get the nth charac...

Page 1 of 26