Tutorial by Examples

Ref returns and ref locals are useful for manipulating and returning references to blocks of memory instead of copying memory without resorting to unsafe pointers. Ref Return public static ref TValue Choose<TValue>( Func<bool> condition, ref TValue left, ref TValue right) { ...
The keyword partial can be used during type definition of class, struct, or interface to allow the type definition to be split into several files. This is useful to incorporate new features in auto generated code. File1.cs namespace A { public partial class Test { public string...
We're going to use the expression tree API to create a CalculateSalesTax tree. In plain English, here's a summary of the steps it takes to create the tree. Check if the product is taxable If it is, multiply the line total by the applicable tax rate and return that amount Otherwise return 0 ...
Determine your key ID gpg --list-secret-keys --keyid-format LONG /Users/davidcondrey/.gnupg/secring.gpg -------------------------------------- sec 2048R/YOUR-16-DIGIT-KEY-ID YYYY-MM-DD [expires: YYYY-MM-DD] Your ID is a alphanumeric 16-digit code following the first forward-slash. ...
Sometimes we want to look at what is sent and received in the SOAP request. The following methods will return the XML in the request and response: SoapClient::__getLastRequest() SoapClient::__getLastRequestHeaders() SoapClient::__getLastResponse() SoapClient::__getLastResponseHeaders() For ex...
array_flip function will exchange all keys with its elements. $colors = array( 'one' => 'red', 'two' => 'blue', 'three' => 'yellow', ); array_flip($colors); //will output array( 'red' => 'one', 'blue' => 'two', 'yellow' => 'three' )
When pasting text through a terminal emulator, the auto-indent feature may destroy the indentation of the pasted text. For example: function () { echo 'foo' echo 'bar' echo 'baz' } will be pasted as: function () { echo 'foo' echo 'bar' echo 'baz' ...
When static_cast is used to convert T D::* to T B::*, the member pointed to must belong to a class that is a base class or derived class of B. Otherwise the behavior is undefined. See Derived to base conversion for pointers to members
The following uses of pointer arithmetic cause undefined behavior: Addition or subtraction of an integer, if the result does not belong to the same array object as the pointer operand. (Here, the element one past the end is considered to still belong to the array.) int a[10]; int* p1 = &a...
For the built-in shift operator, the right operand must be nonnegative and strictly less than the bit width of the promoted left operand. Otherwise, the behavior is undefined. const int a = 42; const int b = a << -1; // UB const int c = a << 0; // ok const int d = a << 32; // ...
C++11 Example from the Standard, [dcl.attr.noreturn]: [[ noreturn ]] void f() { throw "error"; // OK } [[ noreturn ]] void q(int i) { // behavior is undefined if called with an argument <= 0 if (i > 0) throw "positive"; }
In this example, a destructor is explicitly invoked for an object that will later be automatically destroyed. struct S { ~S() { std::cout << "destroying S\n"; } }; int main() { S s; s.~S(); } // UB: s destroyed a second time here A similar issue occurs when a st...
Example from the Standard, [temp.inst]/17: template<class T> class X { X<T>* p; // OK X<T*> a; // implicit generation of X<T> requires // the implicit instantiation of X<T*> which requires // the implicit instantiation of X<T**&...
Single characters can be extracted using array (square brace) syntax as well as curly brace syntax. These two syntaxes will only return a single character from the string. If more than one character is needed, a function will be required, i.e.- substr Strings, like everything in PHP, are 0-indexed....
TestNG requires JDK 7 or higher to use. According to http://testng.org/doc/download.html in order to install testng you need to add testng dependency to your maven pom.xml or gradle build.gradle file Maven: <repositories> <repository> <id>jcenter</id> <name...
Creating a list of KeyValuePair: Dictionary<int, int> dictionary = new Dictionary<int, int>(); List<KeyValuePair<int, int>> list = new List<KeyValuePair<int, int>>(); list.AddRange(dictionary); Creating a list of keys: Dictionary<int, int> dictionary ...
To delete a file (if you have required permissions) is as simple as: File.Delete(path); However many things may go wrong: You do not have required permissions (UnauthorizedAccessException is thrown). File may be in use by someone else (IOException is thrown). File cannot be deleted because ...
To change a text file is not easy because its content must be moved around. For small files easiest method is to read its content in memory and then write back modified text. In this example we read all lines from a file and drop all blank lines then we write back to original path: File.WriteAllLi...
Text is saved encoded (see also Strings topic) then sometimes you may need to change its encoding, this example assumes (for simplicity) that file is not too big and it can be entirely read in memory: public static void ConvertEncoding(string path, Encoding from, Encoding to) { File.WriteAllT...
This example updates last write time of a huge number of files (using System.IO.Directory.EnumerateFiles instead of System.IO.Directory.GetFiles()). Optionally you can specify a search pattern (default is "*.*" and eventually search through a directory tree (not only the specified director...

Page 752 of 1336