public class Dog
{
private const string _birthStringFormat = "yyyy-MM-dd";
[XmlIgnore]
public DateTime Birth {get; set;}
[XmlElement(ElementName="Birth")]
public string BirthString
{
get { return Birth.ToString(_birthStringFormat); }
...
HttpClient is available through NuGet: Microsoft HTTP Client Libraries.
string requestUri = "http://www.example.com";
string responseData;
using (var client = new HttpClient())
{
using(var response = client.GetAsync(requestUri).Result)
{
response.EnsureSuccessStatus...
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 ...
Code can and should throw exceptions in exceptional circumstances. Examples of this include:
Attempting to read past the end of a stream
Not having necessary permissions to access a file
Attempting to perform an invalid operation, such as dividing by zero
A timeout occurring when downloading a...
var persons = new[]
{
new {Id = 1, Name = "Foo"},
new {Id = 2, Name = "Bar"},
new {Id = 3, Name = "Fizz"},
new {Id = 4, Name = "Buzz"}
};
var names = persons.Select(p => p.Name);
Console.WriteLine(string.Join(",", names....
This method returns an IEnumerable with all the elements that meets the lambda expression
Example
var personNames = new[]
{
"Foo", "Bar", "Fizz", "Buzz"
};
var namesStartingWithF = personNames.Where(p => p.StartsWith("F"));
Console.W...
var persons = new[]
{
new {Id = 1, Name = "Foo"},
new {Id = 2, Name = "Bar"},
new {Id = 3, Name = "Fizz"},
new {Id = 4, Name = "Buzz"}
};
var personsSortedByName = persons.OrderBy(p => p.Name);
Console.WriteLine(string.Join(&quo...
var persons = new[]
{
new {Id = 1, Name = "Foo"},
new {Id = 2, Name = "Bar"},
new {Id = 3, Name = "Fizz"},
new {Id = 4, Name = "Buzz"}
};
var personsSortedByNameDescending = persons.OrderByDescending(p => p.Name);
Console.WriteL...
var oneNumber = new[] {5};
var theOnlyNumber = oneNumber.Single();
Console.WriteLine(theOnlyNumber); //5
var numbers = new[] {1,2,3,4,5};
var theOnlyNumberSmallerThanTwo = numbers.Single(n => n < 2);
Console.WriteLine(theOnlyNumberSmallerThanTwo); //1
The following throws Invalid...
var oneNumber = new[] {5};
var theOnlyNumber = oneNumber.SingleOrDefault();
Console.WriteLine(theOnlyNumber); //5
var numbers = new[] {1,2,3,4,5};
var theOnlyNumberSmallerThanTwo = numbers.SingleOrDefault(n => n < 2);
Console.WriteLine(theOnlyNumberSmallerThanTwo); //1
var theOnl...
For classes, interfaces, delegate, array, nullable (such as int?) and pointer types, default(TheType) returns null:
class MyClass {}
Debug.Assert(default(MyClass) == null);
Debug.Assert(default(string) == null);
For structs and enums, default(TheType) returns the same as new TheType():
struct...
The readonly keyword is a field modifier. When a field declaration includes a readonly modifier, assignments to that field can only occur as part of the declaration or in a constructor in the same class.
The readonly keyword is different from the const keyword. A const field can only be initialized...