It is necessary to read this article 1.2 minute .
along with C# Upgrade of ,C# The initialization of objects is simplified in syntax , Let's see if there's anything you don't know .

Initialization of arrays

In the last article, I listed the array , The initialization of arrays is also mentioned , And I'll just quote it .
int[] arr = new int[3] {1, 2, 3}; // Formal initialization
int[] arr = new [] {1, 2, 3}; // Simplified int And array capacity declaration
int[] arr = {1, 2, 3}; // Ultimate simplification
Two initialization methods of dictionary

The second is C# 6 Grammar of , Maybe a lot of people don't know .
// Mode 1 :
var dict = new Dictionary<string, int>
{
{ "key1", 1 },
{ "key2", 20 }
};

// Mode 2 :
var dict = new Dictionary<string, int>
{
["key1"] = 1,
["key2"] = 20
};
Object initialization with custom indexer

This initialization principle is actually the same as the second initialization of the above dictionary .
public class IndexableClass
{
public int this[int index]
{
set
{
Console.WriteLine("{0} was assigned to index {1}", value, index);
}
}
}

var foo = new IndexableClass
{
[0] = 10,
[1] = 20
}
tuple (Tuple) Three initialization methods of

The first two are common , The latter is C# 7 Grammar of , Maybe some people don't know .
// Mode 1 :
var tuple = new Tuple<string, int, MyClass>("foo", 123, new MyClass());

// Mode 2 :
var tuple = Tuple.Create("foo", 123, new MyClass());

// Mode 3 :
var tuple = ("foo", 123, new MyClass());
Add another little knowledge , stay C# 7 in , Elements of tuples can be deconstructed and named :
(int number, bool flage) tuple = (123, true);
Console.WriteLine(tuple.number); // 123
Console.WriteLine(tuple.flag); // True
Initialization of custom collection classes

As long as the custom collection class contains Add method , You can use the following initialization method to initialize elements for a collection .
class Program
{
static void Main()
{
var collection = new MyCollection {
"foo", // Correspondence method :Add(string item)
{ "bar", 3 }, // Correspondence method :Add(string item, int count)
"baz", // Correspondence method :Add(string item)
123.45d, // Corresponding extension method :Add(this MyCollection @this, double value)
};
}
}

class MyCollection : IEnumerable
{
privatereadonly IList _list = new ArrayList();

public void Add(string item)
{
_list.Add(item);
}

public void Add(string item, int count)
{
for (int i = 0; i < count; i++)
{
_list.Add(item);
}
}

public IEnumerator GetEnumerator()
{
return _list.GetEnumerator();
}
}

static class MyCollectionExtensions
{
public static void Add(this MyCollection @this, double value) =>
@this.Add(value.ToString());
}
Object's collection property initialization

We know that initialization of a collection must use the new Create the collection , Cannot be omitted , such as :
// OK
IList<string> synonyms = new List<string> { "c#", "c-sharp" };

// Compile error , Cannot be omitted new List<string>
IList<string> synonyms = { "c#", "c-sharp" };
But if the collection is a property of another class , Can be omitted new, such as :
public class Tag
{
public IList<string> Synonyms { get; set; }
}

var tag = new Tag
{
Synonyms = { "c#", "c-sharp" } // OK
};
That's all I can think of and find , I hope the above will be helpful to your programming .

Technology
©2019-2020 Toolsou All rights reserved,
QCustomPlot series (5)- Real time dynamic curve python To solve the problem of dictionary writing list in GDOI2019 travels Java Basics ( Bubble sort )Unity Scene loading asynchronously ( Implementation of loading interface )" Cephalosporin wine Say go and go "? DANGER ! Don't drink alcohol when taking these drugs 2021 year 1 Monthly programmer salary statistics , average 14915 element Huawei certification HCIA-AI artificial intelligence java Simple lottery algorithm , luck draw DemoNOI2019 travels