concept System in category .net

This is an excerpt from Manning's book .NET Core in Action.
Listing 3.2. Contents of the CsvReader.cs file
using System.Collections.Generic; using System.IO; namespace CsvParser { public class CsvReader { private string[] columns; private TextReader source; public CsvReader(TextReader reader) { this.source = reader; var columnLine = reader.ReadLine(); this.columns = columnLine.Split(','); #1 } public IEnumerable<KeyValuePair<string, string>[]> Lines { get { string row; #2 while ((row = this.source.ReadLine()) != null) { var cells = row.Split(','); #3 var pairs = new KeyValuePair<string, string>[columns.Length]; for (int col = 0; col < columns.Length; col++) { pairs[col] = new KeyValuePair<string, string>( columns[col], cells[col]); } yield return pairs; #4 } } } } }
The zlib library is not only the de-facto standard for GZip compression/decompression, it is also implemented in native code, which gives it a slight performance advantage over any managed .NET implementation. Given that zlib is a native library, the code in System.IO.Compression has to behave differently based on the runtime.
Listing 14.2. Program.cs for our test of the .NET Portability Analyzer
using System; using System.Diagnostics; using System.IO; using System.Net; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { string uri = "http://www.bing.com"; var firstRequest = MeasureRequest(uri); var secondRequest = MeasureRequest(uri); if (firstRequest.Item1 != HttpStatusCode.OK && secondRequest.Item1 != HttpStatusCode.OK) { Console.WriteLine("Unexpected status code"); } else { Console.WriteLine($"First request took {firstRequest.Item2}ms"); Console.WriteLine($"Second request took {secondRequest.Item2}ms"); } Console.ReadLine(); } static Tuple<HttpStatusCode, long> MeasureRequest(string uri) { #1 var stopwatch = new Stopwatch(); var request = WebRequest.Create(uri); request.Method = "GET"; stopwatch.Start(); using (var response = request.GetResponse() as HttpWebResponse) { using (var reader = new StreamReader(response.GetResponseStream())) { reader.ReadToEnd(); #2 stopwatch.Stop(); } return new Tuple<HttpStatusCode, long>( response.StatusCode, stopwatch.ElapsedMilliseconds); } } } } #1 MeasureRequest measures the latency of an HTTP request #2 Make sure we’ve read the whole response

This is an excerpt from Manning's book C# in Depth.
Listing 1.1. The Product type (C# 1)
using System.Collections; public class Product { string name; public string Name { get { return name; } } decimal price; public decimal Price { get { return price; } } public Product(string name, decimal price) { this.name = name; this.price = price; } public static ArrayList GetSampleProducts() { ArrayList list = new ArrayList(); list.Add(new Product("West Side Story", 9.99m)); list.Add(new Product("Assassins", 14.99m)); list.Add(new Product("Frogs", 13.99m)); list.Add(new Product("Sweeney Todd", 10.99m)); return list; } public override string ToString() { return string.Format("{0}: {1}", name, price); } }
This time ValSample is a reference type, despite T being constrained to be a value type. Note that System.Enum and System.ValueType are both reference types in themselves, so they aren’t allowed as valid type arguments for ValSample. When a type parameter is constrained to be a value type, comparisons using == and != are prohibited.

This is an excerpt from Manning's book IronPython in Action.
dir(object) will give you a list of all the attributes available on an object. It returns a list of strings, so you can filter it or do anything else you might do with a list. The following code snippet looks for all the interfaces in the System.Collections namespace by building a list and then printing all the members whose name begins with I.
Listing 10.7. Querying the system with ManagementObjectSearcher
import clr clr.AddReference("System.Management") from System.Management import ManagementObjectSearcher query = "Select * from Win32_LogicalDisk" searcher = ManagementObjectSearcher(query) for drive in searcher.Get(): for p in drive.Properties: print p.Name, p.Value print
Although WMI provides you with access to some very low-level system information, it has a good high-level managed interface, in the form of the System.Management namespace. This makes it easier to work with WMI through .NET and IronPython than some of the alternatives.

This is an excerpt from Manning's book LINQ in Action.
The Table attribute is provided by LINQ to SQL in the System.Data.Linq.Mapping namespace. It has a Name property that is used to specify the name of the database table.
Let’s say we want to get a list of the processes running on our computer. This can be done easily thanks to the System.Diagnostics.Process.GetProcesses API.
Let’s say that we’d like to sort the list of processes by their memory consumption, memory hogs first. We can use the OrderByDescending extension method defined in the System.Linq.Enumerable class. Extension methods are imported through using namespace directives. For example, to use the extension methods defined in the Enumerable class, we need to add the following line of code to the top of our code file if it’s not already there:
using System.Linq;
We regret that MethodInvoker has been declared in the System.Windows.Forms namespace even though it can be useful outside Windows Forms applications. This has been addressed in .NET 3.5. A new version of the Action delegate type that takes no parameter is added to the System namespace by the new System.Core.dll assembly: