C#快速构建代码块 简写功能总结表格
简写 |
功能 |
示例 |
prop |
创建属性代码块 |
publicint MyProperty {get;set; } |
cw |
创建对Console.WriteLine();的调用 |
Console.WriteLine(); |
ctor |
创建构造函数 |
public Program (){} |
~ |
创建析构函数 |
~Program(){} |
svm |
创建static viod函数声明 |
staticvoid Main(string[] args){} |
|
|
|
using |
创建using代码块 |
using (resource){} |
while |
创建while循环 |
while (true){} |
unsafe |
创建unsafe代码块
|
unsafe{}
|
checked |
创建checked块 |
checked{} |
unchecked |
创建unchecked代码块 |
unchecked{} |
switch
|
创建switch代码块 |
switch (switch_on){default:}
|
struct
|
创建struct声明 |
struct MyStruct{}
|
namespace |
创建namespace声明 |
namespace MyNamespace{} |
lock
|
创建lock块 |
lock (this){} |
interface |
创建interface声明 |
interfaceIInterface{} |
if |
创建if块 |
if (true){} |
for |
创建递增for循环 |
for (int i = 0; i < length; i++){} |
forr |
创建递减for循环 |
for (int i = length - 1; i >= 0; i--){} |
foreach |
创建foreach循环 |
foreach (var itemin collection){} |
enum |
创建enum声明 |
enumMyEnum{} |
else |
创建else块 |
else{} |
do |
创建do(while)循环 |
do{} while (true); |
class |
创建类声明 |
classMyClass{} |
用装饰器模式实现AOP
Person类只会洗澡,而PersonDecorator类可以实现:脱衣服→洗澡→穿衣服,而无需改变原来的Person类;
public interface IPerson
{
public void Bath();
}
public class Person:IPerson
{
public string Name { get; set; }
public Person(string name) => Name = name;
public void Bath() => Console.WriteLine($"我叫{Name},我要洗澡啦");
}
public class PersonDecorator:IPerson
{
public IPerson Person { get; }
public PersonDecorator(IPerson person)=> Person = person;
public void TakeOff() => Console.WriteLine("脱衣服。。。");
public void PutOn() => Console.WriteLine("穿衣服。。。");
public void Bath()
{
TakeOff();
Person.Bath();
PutOn();
}
}
static void Main(string[] args)
{
var person = new Person("刘老根");
person.Bath();
Console.WriteLine("-----------------------");
var personDecorator=new PersonDecorator(person);
personDecorator.Bath();
}
我叫刘老根,我要洗澡啦
-----------------------
脱衣服。。。
我叫刘老根,我要洗澡啦
穿衣服。。。
LINQ操作符总结
分类 |
操作符 |
说明 |
条件类 |
Where |
用lambda表达式作为查询条件names.Where(n => n.StartsWith("J")) |
OfType<TResult> |
按照数据类型进行查询, |
|
选择类 |
Select |
names.Where(n => n.StartsWith("J")).OrderBy(n => n).Select(n => n) |
SelectMany |
二级选择,从中间选择结果中进行进一步选择 |
|
排序类 |
OrderBy |
按照某个字段或属性进行排序 |
ThenBy |
二级排序,先按照某个字段进行排序,然后按照另一个字段再次裴谞 |
|
OrderByDecending |
降序排序 |
|
ThenByDecending |
二级排序,降序 |
|
Reverse |
反转顺序,倒序 |
|
合并类 |
Join |
合并不直接相关的集合 |
GroupJoin |
连接两个集合,组合其结果 |
|
分组类 |
GroupBy |
分组from book in Books group book by book.Publisher into grouping |
ToLookup |
通过创建一对多字典,来组合元素 |
|
布尔类 |
Any |
任意一个元素满足条件,就返回true |
All |
所有元素均满足条件,才返回true |
|
Contains |
包含某个元素,就返回true |
|
分区类 |
Take |
获得指定数量的结果 |
Skip |
跳过指定数量的结果 |
|
TakeWhile |
获取满足指定条件的,指定个数的结果 |
|
SkipWhile |
跳过满足指定条件的,指定个数的结果 |
|
复合类 |
Distinct |
删除重复元素,去重 |
Union |
合并两个集合并去重,相当于先并集再去重 |
|
Intersect |
交集 |
|
Zip |
并集 |
|
Except |
差集 |
|
索引类 |
First |
满足条件的第一个元素 |
FirstOrDefault |
满足条件的第一个元素,如果找不到,就返回元素的默认值 |
|
Last |
满足条件的最后一个元素 |
|
LastOrDefault |
满足条件的最后一个元素,如果找不到,就返回元素的默认值 |
|
ElementAt |
按照索引查找元素 |
|
ElementAtOrDefault |
按照索引查找元素,如果找不到,就返回元素的默认值 |
|
Single |
只返回一个满足条件的元素,如果找到多个,就抛出异常 |
|
SingleOrDefault |
只返回一个满足条件的元素,如果找不到,就返回元素的默认值 |
|
统计类 |
Count |
元素个数 |
Sum |
所有元素求和var query = nums.Sum(i => (long)i); |
|
Min |
最小值 |
|
Max |
最大值 |
|
Average |
平均值 |
|
Aggregate |
总计 int[] numbers = new int[] { 1, 2, 3, 4 }; |
|
转换类 |
ToArray |
转换为数组 |
AsEnumerable |
转换为IEnumerable子类 |
|
ToList |
转换为IList子类 |
|
ToDictionary |
转换为IDictionary子类 |
|
Cast<TResult> |
转换为指定类型 |
|
初始类 |
Empty |
生成一个空集合 |
Range |
生成一系列数字 |
|
Repeate |
生成一个元素都是同一个值的集合 |
Linq查询的延迟执行
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleCore230207
{
class Program
{
static void Main(string[] args)
{
DeferredQuery();
DeferredQuery2();
}
//延迟查询函数 Linq表达式形式
static void DeferredQuery()
{
//定义一个集合
var names = new List<string> { "Bert", "Jack", "Rose", "Tom" };
//定义Linq表达式 此时仅仅定义了表达式,而未执行
//Linq表达式在遍历时才执行
//{System.Linq.OrderedEnumerable<string, string>}
var namesWithJ = from n in names where (n.StartsWith("J")) orderby n select n;
Console.WriteLine("第1次遍历:");
//遍历时,执行Linq表达式
foreach (var item in namesWithJ)
{
Console.WriteLine(item);
}
names.Add("Json");
names.Add("Jerry");
names.Add("Job");
Console.WriteLine("第2次遍历:");
//遍历时,再次执行Linq表达式,此次输出更新集合上的查询结果
foreach (var item in namesWithJ)
{
Console.WriteLine(item);
}
}
//扩展方法式的 延迟查询
static void DeferredQuery2()
{
//定义一个集合
var names = new List<string> { "Bert", "Jack", "Rose", "Tom" };
//扩展方法形式的Linq查询
//Linq查询的扩展方法在遍历时才执行
//namesWithJ的数据类型是枚举器{System.Linq.Enumerable.SelectIPartitionIterator<string, string>}
var namesWithJ = names.Where(n => n.StartsWith("J")).OrderBy(n => n).Select(n => n);
Console.WriteLine("第1次遍历:");
//遍历时,执行Linq表达式
foreach (var item in namesWithJ)
{
Console.WriteLine(item);
}
names.Add("Json");
names.Add("Jerry");
names.Add("Job");
Console.WriteLine("第2次遍历:");
//遍历时,再次执行Linq表达式,此次输出更新集合上的查询结果
foreach (var item in namesWithJ)
{
Console.WriteLine(item);
}
}
//两种方法的输出结果相同;表达式形式和扩展方法形式的Linq查询,是等价的;
//第1次遍历:
//Jack
//第2次遍历:
//Jack
//Jerry
//Job
//Json
}
}
#end
标签:Console,C#,创建,编程,高级,Linq,names,WriteLine,public From: https://www.cnblogs.com/zhangdezhang/p/17099088.html