Tutorial by Examples: ect

Initialize a collection type with values: var stringList = new List<string> { "foo", "bar", }; Collection initializers are syntactic sugar for Add() calls. Above code is equivalent to: var temp = new List<string>(); temp.Add("foo"); temp.A...
public void SerializeFoo(string fileName, Foo foo) { var serializer = new XmlSerializer(typeof(Foo)); using (var stream = File.Open(fileName, FileMode.Create)) { serializer.Serialize(stream, foo); } }
public Foo DeserializeFoo(string fileName) { var serializer = new XmlSerializer(typeof(Foo)); using (var stream = File.OpenRead(fileName)) { return (Foo)serializer.Deserialize(stream); } }
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....
var numbers1to10 = new[] {1,2,3,4,5,6,7,8,9,10}; var numbers5to15 = new[] {5,6,7,8,9,10,11,12,13,14,15}; var numbers5to10 = numbers1to10.Intersect(numbers5to15); Console.WriteLine(string.Join(",", numbers5to10)); //5,6,7,8,9,10
Enumerable.Select returns an output element for every input element. Whereas Enumerable.SelectMany produces a variable number of output elements for each input element. This means that the output sequence may contain more or fewer elements than were in the input sequence. Lambda expressions passe...
To make a class support collection initializers, it must implement IEnumerable interface and have at least one Add method. Since C# 6, any collection implementing IEnumerable can be extended with custom Add methods using extension methods. class Program { static void Main() { va...
You are allowed to create and throw exceptions in your own code. Instantiating an exception is done the same way that any other C# object. Exception ex = new Exception(); // constructor with an overload that takes a message string Exception ex = new Exception("Error message"); Yo...
// Translates to `dict.Add(1, "First")` etc. var dict = new Dictionary<int, string>() { { 1, "First" }, { 2, "Second" }, { 3, "Third" } }; // Translates to `dict[1] = "First"` etc. // Works in C# 6.0. var dict = new Dicti...
var letters = "letters".ToCharArray(); char letter = letters[1]; Console.WriteLine("Second Letter is {0}",letter); //in the above example we take the second character from the array //by calling letters[1] //NB: Array Indexing starts at 0; i.e. the first letter would be give...
Starting from a new Settings class and custom configuration section: Add an application setting named ExampleTimeout, using the time System.Timespan, and set the value to 1 minute: Save the Project Properties, which saves the Settings tab entries, as well as re-generates the custom Settings cl...
var collection = new BlockingCollection<int>(5); var random = new Random(); var producerTask = Task.Run(() => { for(int item=1; item<=10; item++) { collection.Add(item); Console.WriteLine("Produced: " + item); Thread.Sleep(random.Next(1...
See the other (Basic) examples above. using System.ComponentModel.Composition; using System.ComponentModel.Composition.Hosting; namespace Demo { public static class Program { public static void Main() { using (var catalog = new ApplicationCatalog()) ...
An iterator method is not executed until the return value is enumerated. It's therefore advantageous to assert preconditions outside of the iterator. public static IEnumerable<int> Count(int start, int count) { // The exception will throw when the method is called, not when the result i...
Select allows you to apply a transformation to every element in any data structure implementing IEnumerable. Getting the first character of each string in the following list: List<String> trees = new List<String>{ "Oak", "Birch", "Beech", "Elm", ...
Install the NuGet package System.Reactive, then add this using statement to access the Rx extension methods: using System.Reactive.Linq;
var sequenceOfSequences = new [] { new [] { 1, 2, 3 }, new [] { 4, 5 }, new [] { 6 } }; var sequence = sequenceOfSequences.SelectMany(x => x); // returns { 1, 2, 3, 4, 5, 6 } Use SelectMany() if you have, or you are creating a sequence of sequences, but you want the result as one long sequen...
The SelectMany linq method 'flattens' an IEnumerable<IEnumerable<T>> into an IEnumerable<T>. All of the T elements within the IEnumerable instances contained in the source IEnumerable will be combined into a single IEnumerable. var words = new [] { "a,b,c", "d,e&quo...
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...

Page 1 of 99