C# 7
2020-03-19 20:08
155 查看
https://www.cnblogs.com/Fred1987/archive/2020/01/28/12238973.html
static void LocalMethod() { Cube(100); void Cube(int x) => Console.WriteLine($"The cube of {x} is {x * x * x}"); } static void GoToDemo() { int i = 1; startLoop: if(i<=5) { Console.WriteLine(i); i++; goto startLoop; } }
deconstructor
class Rectangle { public readonly float Width, Height; public Rectangle(float width,float height) { Width = width; Height = height; } public void Deconstruct(out float width,out float height) { width = Width; height = Height; } } static void Main(string[] args) { var rect = new Rectangle(3, 4); (float width, float height) = rect; Console.WriteLine($"width:{width},height:{height}"); Console.ReadLine(); }
static void Main(string[] args) { var rect = new Rectangle(3, 4); var (width, height) = rect; Console.WriteLine($"width:{width},height:{height}"); Console.ReadLine(); }
static void Main(string[] args) { var rect = new Rectangle(3, 4); (var width,var height) = rect; Console.WriteLine($"width:{width},height:{height}"); Console.ReadLine(); }
class Product { decimal currentPrice, sharesOwned; public decimal Worth { get { return currentPrice * sharesOwned; } } public decimal Worth2 => currentPrice * sharesOwned; public decimal Worth3 { get => currentPrice * sharesOwned; set => sharesOwned = value / currentPrice; }
public decimal CurrentPrice { get; set; } = 123;
public int Maximum { get; } = 999;
}
private decimal x; public decimal X { get { return x; } private set { x = Math.Round(value, 2); } }
Indexer
class Sentence { string[] words = "The quick brown fox".Split(); public string this[int wordNum] { get { return words[wordNum]; } set { words[wordNum] = value; } } } static void Main(string[] args) { Sentence se = new Sentence(); Console.WriteLine(se[3]); se[3] = "kangaroo"; Console.WriteLine(se[3]); Console.ReadLine(); }
static class StaticClass { static StaticClass() { Console.WriteLine("This is the static constructor of the static class!"); } public static DateTime DT = DateTime.Now; }
Partial class,partial method.
partial class PaymentForm { partial void ValidatePayment(decimal amount); public void InvokePartialMethod(decimal amount) { ValidatePayment(amount); } } partial class PaymentForm { partial void ValidatePayment(decimal amount) { if(amount>100) { Console.WriteLine("Luxury"); } else { Console.WriteLine("Ok"); } } } static void Main(string[] args) { PaymentForm pf = new PaymentForm(); pf.InvokePartialMethod(10); pf.InvokePartialMethod(101); Console.ReadLine(); }
internal class Countdown : IEnumerator { int count = 11; public object Current => count; public bool MoveNext() => count-- > 0; public void Reset() { throw new NotImplementedException(); } } static void Main(string[] args) { IEnumerator e = new Countdown(); while(e.MoveNext()) { Console.WriteLine(e.Current); } Console.ReadLine(); }
[Flags] public enum BorderSides { None=0,Left=1,Right=2,Top=4,Bottom=8 } static void Main(string[] args) { BorderSides leftRight = BorderSides.Left | BorderSides.Right; if((leftRight&BorderSides.Left)!=0) { Console.WriteLine("Includes Left!"); } string formatted = leftRight.ToString(); BorderSides s = BorderSides.Left; s |= BorderSides.Right; Console.WriteLine(s == leftRight); Console.ReadLine(); }
[Flags] public enum BSS { None=0,Left=1,Right=2,Top=4,Bottom=8, LeftRight=Left|Right, TopBottom=Top|Bottom, All=LeftRight|TopBottom }
Nested types
public class TopLevel { public class Nested { } public enum Color { Red,Blue,Tan} } static void Main(string[] args) { TopLevel.Color color = TopLevel.Color.Red; Console.WriteLine(color); Console.ReadLine(); }
Generics express reusability with a "Template" that contains "placeholder" types.Generics when compared to inheritance,can increase type safety and reduce casting and boxing.
public class Stack<T> { int pos; T[] data = new T[100]; public void Push(T obj) => data[pos++] = obj; public T Pop() => data[--pos]; }
static void Main(string[] args)
{
var stack = new Stack<int>();
stack.Push(5);
stack.Push(10);
WriteLine(stack.Pop());
WriteLine(stack.Pop());
Console.ReadLine();
}
static void Main(string[] args) { Func<int,int,int> fc = AddXY; int result = AddXY(10, 20); Console.WriteLine(result); Console.ReadLine(); } private static int AddXY(int v1, int v2) { return v1 + v2; }
相关文章推荐
- C#与Java的RSA(2)
- c# 类型转换
- C#和java里提供的原生的压缩方法
- C# 简单读写 EXCEL
- LINQ之路 3:C# 3.0的语言功能(下)
- C#多态
- C#生成XML的三种途径小结
- C#接口相关知识
- c# 之 New新知
- C#ref 使用
- C#面试题1~100
- c#从控制台输入问题
- C#中的foreach循环
- 【小松教你手游开发】【面试必读(编程基础)】C# 理解泛型
- c# 数据库的 sql 参数封装类的编写
- C#扩展方法实现 byte[] 输出为HEX字符串形式
- C#继承和多态
- 每一个C#开发者必须知道的13件事情
- c# 泛型(一)--引入
- C#OLEDB读Excel单元格的值的方法