1. var implicit type
var is a special variable type, which can be used to represent any type of variable
be careful:
(1) var cannot be used as a member of a class. It can only be used to declare temporary variables, that is, it is usually written in a function statement block
(2) var must be initialized, but the type is determined after initialization
var i = 5; var s = "srea"; var array = new int[] { 1, 2, 3, 4 }; var list = new List<int>();
2. Set object initial value
When declaring an object, you can initialize public member variables and attributes by directly writing braces
class Person { private int money; public bool sex; public string Name { get; set; } public int Age { get; set; } } Person p = new Person { sex = true, Age = 17, Name = "person" }; //Parameterless construction parentheses can be omitted Person p2 = new Person() { sex = true };
If it is a parameterized structure, you can directly pass in the relevant parameters in parentheses
3. Set set initial value
When declaring a collection object, you can also directly initialize the internal properties through braces
int[] array = new int[] { 1, 2, 3, 4, 5 }; List<int> list = new List<int>() { 1, 2, 3, 4, 5 }; // Parentheses can also be omitted List<Person> listPerson = new List<Person>() { new Person(), new Person(){Age = 10}, new Person(){sex = true, Name = "test"} }; Dictionary<int, string> dic = new Dictionary<int, string>() { {1, "123" }, {2, "222" } };
4. Anonymous type
var variables can be declared as custom anonymous types
var v = new { age = 10, money = 11, name = "Ming" }; Console.WriteLine("{0}, {1}, {2}", v.age, v.money, v.name);
Cannot add a Lambda expression to an anonymous type
5. Nullable type
(1) The value type cannot be assigned null, that is, int a = null; No
(2) When declaring, add?, after the value type?, Can be assigned null
int? a = null;
(3) Judge whether it is empty
if (a.HasValue) { Console.WriteLine(a); // Equivalent to Console.WriteLine(a.Value); }
Note: a cannot be directly assigned to a variable of type int, that is, int v = a; Is an error, and the corresponding Value must be obtained through Value: int v = a.Value;
(4) Safely get nullable type value
If blank, the default value of the default return value type
Console.WriteLine(a.GetValueOrDefault());
You can also specify a default value
Console.WriteLine(a.GetValueOrDefault(100));
But the value of a is not assigned to 100
(5) Usage with reference type:
object o = null; if (o != null) { o.ToString(); } Action action = null; if (action != null) action.Invoke();
Can be abbreviated as
o?.ToString(); action?.Invoke();
For arrays:
int[] array = null; Console.WriteLine(array?[0]);
It is equivalent to a syntax sugar, which can help automatically determine whether it is empty. If it is empty, the following methods will not be executed
6. Empty merge operator
Empty merge operator??
Format: left value?? Right value
If the left value is null, the right value is returned; otherwise, the left value is returned
Any null able type can be used
Similar to trinocular operator
int? intV = null; int intI = intV == null ? 100 : intV.Value; intI = intV ?? 100;
7. Interpolate string
Key symbols:$
Use $to construct a string, so that variables can be spliced in the string
string name = "ming"; int age = 19; Console.WriteLine($"hello world,{name},{age}");
8. A brief description of single sentence logic
It is generally said that braces can be omitted from a single sentence, mainly referring to the writing of attributes and functions in a class
public string Name { get => "test"; set => name = value; } public int Add(int a, int b) => a + b; public void Speak(string str) => Console.WriteLine(str);