var i = 5;
var s = "Hello";
var d = 1.0;
var numbers = new int[] {1, 2, 3};
var orders = new Dictionary<int,Order>();
List<Pair<String, Double>> scores = seeker.getScores(documentAsCentroid);
...
foreach(Pair<String, Double> score in scores)
using StopWordsTables = System.Collections.Generic.List<System.Collections.Generic.Dictionary<string, string>>;
var r = new Rectangle {
P1 = new Point { X = 0, Y = 1 },
P2 = new Point { X = 2, Y = 3 }
};
List<int> digits = new List<int> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
public Class Point {
private int x;
private int y;
public int X { get { return x; } set { x = value; } }
public int Y { get { return y; } set { y = value; } }
}
public Class Point {
public int X { get; set; }
public int Y { get; set; }
}
var p1 = new { Name = "Lawnmower", Price = 495.00 };
var p2 = new { Name = "Shovel", Price = 26.95 };
p1 = p2;
namespace Acme.Utilities
{
public static class Extensions
{
public static int ToInt32(this string s) {
return Int32.Parse(s);
}
public static T[] Slice<T>(this T[] source, int index, int count) {
if (index < 0 || count < 0 || source.Length – index < count)
throw new ArgumentException();
T[] result = new T[count];
Array.Copy(source, index, result, 0, count);
return result;
}
}
}
using Acme.Utilities;
...
string s = "1234";
int i = s.ToInt32(); // Same as Extensions.ToInt32(s)
int[] digits = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int[] a = digits.Slice(4, 3); // Same as Extensions.Slice(digits, 4, 3)
string[] people = new [] { "Tom", "Dick", "Harry" };
//
var filteredPeople = from p in people where p.Length > 3 select p;
//
var filteredPeople = people.Where (p => p.Length > 3);
x => x + 1 // Implicitly typed, expression body
x => { return x + 1; } // Implicitly typed, statement body
(int x) => x + 1 // Explicitly typed, expression body
(int x) => { return x + 1; } // Explicitly typed, statement body
(x, y) => x * y // Multiple parameters
() => Console.WriteLine() // No parameters
var doc = new XmlDocument("/path/to/file.xml");
var baz = doc.GetElement("foo").GetElement("qux");
dynamic doc = new XmlDocument("/path/to/file.xml");
var baz = doc.foo.qux;
class Car {
public void Accelerate(
double speed, int? gear = null,
bool inReverse = false) {
/* ... */
}
}
Car myCar = new Car();
myCar.Accelerate(55);
public async Task ReadFirstBytesAsync(string filePath1, string filePath2)
{
using (FileStream fs1 = new FileStream(filePath1, FileMode.Open))
using (FileStream fs2 = new FileStream(filePath2, FileMode.Open))
{
await fs1.ReadAsync(new byte[1], 0, 1); // 1
await fs2.ReadAsync(new byte[1], 0, 1); // 2
}
}
public void DoProcessing()
{
TraceMessage("Something happened.");
}
public void TraceMessage(string message,
[System.Runtime.CompilerServices.CallerMemberName] string memberName = "",
[System.Runtime.CompilerServices.CallerFilePath] string sourceFilePath = "",
[System.Runtime.CompilerServices.CallerLineNumber] int sourceLineNumber = 0)
{
System.Diagnostics.Trace.WriteLine("message: " + message);
System.Diagnostics.Trace.WriteLine("member name: " + memberName);
System.Diagnostics.Trace.WriteLine("source file path: " + sourceFilePath);
System.Diagnostics.Trace.WriteLine("source line number: " + sourceLineNumber);
}
// Sample Output:
// message: Something happened.
// member name: DoProcessing
// source file path: c:\Users\username\Documents\Visual Studio 2012\Projects\CallerInfoCS\CallerInfoCS\Form1.cs
// source line number: 31
using static System.Console;
using static System.Math;
using static System.DayOfWeek;
class Program
{
static void Main()
{
WriteLine(Sqrt(3*3 + 4*4));
WriteLine(Friday - Monday);
}
}
try { … }
catch (MyException e) when (myfilter(e))
{
…
}
public class Customer
{
public string First { get; set; } = "Jane";
public string Last { get; set; } = "Doe";
}
public class Customer
{
public string First { get; } = "Jane";
public string Last { get; } = "Doe";
}
public string Name => First + " " + Last;
public Customer this[long id] => store.LookupCustomer(id);
public static string Truncate(string value, int length)
{
return value?.Substring(0, Math.Min(value.Length, length));
}
“Total lines: “ + totalLines + ”, total words: “ + totalWords + ”.”;
if (x == null) throw new ArgumentNullException(nameof(x));
WriteLine(nameof(person.Address.ZipCode)); // prints "ZipCode"
var numbers = new Dictionary<int, string> {
[7] = "seven",
[9] = "nine",
[13] = "thirteen"
};
int x = 0b1010000;
int SeventyFive = 0B10_01011;
public void Foo(int z)
{
void Init()
{
Boo = z;
}
Init();
}
// type pattern
public void Foo(object item)
{
if (item is string s)
{
WriteLine(s.Length);
}
}
// Var Pattern
public void Foo(object item)
{
if(item is var x)
{
WriteLine(item == x); // prints true
}
}
// Wildcard Pattern
public void Foo(object item)
{
if(item is *)
{
WriteLine("Hi there"); //will be executed
}
}
static void Main()
{
var arr = new[] { 1, 2, 3, 4 };
ref int Get(int[] array, int index)=> ref array[index];
ref int item = ref Get(arr, 1);
WriteLine(item);
item = 10;
WriteLine(arr[1]);
ReadLine();
}
// Will print
// 2
// 10
Source: https://habr.com/ru/post/302076/
All Articles