您的位置:首页 > 编程语言 > C#

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;
}

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  gt readline