读书笔记:Pro ASP.NET Core MVC 2 [Chap4 C#重要语言特性]

第四章 C#语言特性

内容:

  1. ?. ??
  2. 格式化字符串
  3. 初始化器
  4. Pattern Matching
  5. 扩展方法
  6. lambda表达式
  7. 匿名类行
  8. 异步操作
  9. 获取名称

使用Empty模板创建ASP.NET Core
空模板创建的项目,包括了最小的 ASP.NET Core configuration ,其中不包括MVC支持,所以需要添加点代码:

public class Startup {
    public void ConfigureServices(IServiceCollection services) {
        services.AddMvc();
    }
    public void Configure(IApplicationBuilder app, IHostingEnvironment env) {
        if (env.IsDevelopment()) {
            app.UseDeveloperExceptionPage();
        }
    //app.Run(async (context) => {
    // await context.Response.WriteAsync("Hello World!");
    //});
        app.UseMvcWithDefaultRoute();
    }
}

Null Conditional Operator ?.

string name = p?.Name;          //若p为null,name==null; 否则 name = p.Name
decimal? price = p?.Price;      //


string relatedName = p?.Related?.Name;    //多个?. 链式使用

null coalescing operator ??

string name = p?.Name ?? "<No Name>";    //若 p?.Name 返回的是null,则 name = "<No Name>"; 否则name = p.Name
decimal? price = p?.Price ?? 0;
string relatedName = p?.Related?.Name ?? "<None>";

Automatically Implemented Properties

public string Name { get; set; }
public decimal? Price { get; set; }
public Product Related { get; set; }

public string Category { get; set; } = "Watersports";  // 自动属性+初始化

格式化字符串

string.Format("Name: {0}, Price: {1}, Related: {2}",name, price, relatedName)

$"Name: {name}, Price: {price}, Related: {relatedName}"

初始化器

Product kayak = new Product {          //对象初始化
Name = "Kayak",
Category = "Water Craft",
Price = 275M
};


new string[] { "Bob", "Joe", "Alice" }    //collection初始化


Dictionary<string, Product> products = new Dictionary<string, Product> {    //字典初始化
    { "Kayak", new Product { Name = "Kayak", Price = 275M } },
    { "Lifejacket", new Product{ Name = "Lifejacket", Price = 48.95M } }
};

Dictionary<string, Product> products = new Dictionary<string, Product> {    //更新的字典初始化方式
    ["Kayak"] = new Product { Name = "Kayak", Price = 275M },
    ["Lifejacket"] = new Product { Name = "Lifejacket", Price = 48.95M }
};

Pattern Matching

object[] data = new object[] { 275M, 29.95M,"apple", "orange", 100, 10 };
if (data[i] is decimal d)     // 如果类型匹配,则值会给变量d


switch (data[i]) {                  //在switch中使用 pattern matching
    case decimal decimalValue:
    total += decimalValue;
    break;
    case int intValue when intValue > 50:     // when 关键字来增加更多限制
    total += intValue;
    break;
}

Extension Method

public static decimal TotalPrices(this ShoppingCart cartParam) {
    decimal total = 0;
    foreach (Product prod in cartParam.Products) {
    total += prod?.Price ?? 0;
    }
    return total;
}


// 给接口的扩展方法,这样所有实现这个接口的都可以使用
public static class MyExtensionMethods {
    public static decimal TotalPrices(this IEnumerable<Product> products) {

    decimal total = 0;

    foreach (Product prod in products) {
        total += prod?.Price ?? 0;
    }

    return total;
    }
}

//使用`yield`关键字来过滤内容
//返回值要是 IEnumerable<...>
public static IEnumerable<Product> FilterByPrice(this IEnumerable<Product> productEnum, decimal minimumPrice) {

    foreach (Product prod in productEnum) {
        if ((prod?.Price ?? 0) >= minimumPrice) {
            yield return prod;
        }
    }
}

Lambda Expressions

prod => EvaluateProduct(prod)      /单参数,单语句

(prod, count) => prod.Price > 20 && count > 0      //多参数

(prod, count) => {                      //多参数,多语句
// ...multiple code statements...
return result;
}


//对方法使用lambda--- 当方法或者constructor中只有一条语句的时候
public ViewResult Index() =>  View(Product.GetProducts().Select(p => p?.Name));


//对property使用lambda
public bool NameBeginsWithS => Name?[0] == 'S';    //只有get的属性

类型推断和匿名类型

var names = new [] { "Kayak", "Lifejacket", "Soccer ball" };   //implicit type

var name = new {Name= "", Age = ""};

异步方法

直接和Task打交道

public static Task<long?> GetPageLength() {
    HttpClient client = new HttpClient();
    var httpTask = client.GetAsync("http://apress.com");
    return httpTask.ContinueWith((Task<HttpResponseMessage> antecedent) => {
        return antecedent.Result.Content.Headers.ContentLength;
    });
}

.NET把每个异步执行的任务表示为Task, Task是强类型的,都是和最终返回什么类型的结果相关。 所以当HttpClient.GetAsync调用的时候,会返回一个Task<HttpResponseMessage>,这个类型意思是, 这个在后台进行的异步任务最终会返回一个HttpResponseMessage对象作为结果。

一般让人迷惑的地方在于 ContinuationContinueWith方法用来处理当Task完成之后,应该做什么操作。

第一个return代表 GetPageLength方法立即返回一个 Task<HttpResponseMessage对象; 第二个在return代表这个Task完成后最后返回的内容。

使用asyncawait

public async static Task<long?> GetPageLength() {
    HttpClient client = new HttpClient();
    var httpMessage = await client.GetAsync("http://apress.com");
    return httpMessage.Content.Headers.ContentLength;
}

public async Task<ViewResult> Index() {
    long? length = await MyAsyncMethods.GetPageLength();
    return View(new string[] { $"Length: {length}" });
}

获取名称 nameof表达式
不是硬编码,而是让编译器来针对属性,类型等等,来产生对应的名字

$"{nameof(p.Name)}: {p.Name}, {nameof(p.Price)}: {p.Price}"));
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Pro ASP.NET Core MVC, 6th Edition ADAM FREEMAN | 2016 | EPUB, True PDF | ISBN: 1484203984 | 1018 pages | 7 Mb, 31 Mb Now in its 6th edition, the best selling book on MVC is now updated for ASP.NET Core MVC. It contains detailed explanations of the new Core MVC functionality which enables developers to produce leaner, cloud optimized and mobile-ready applications for the .NET platform. This book puts ASP.NET Core MVC into context and dives deep into the tools and techniques required to build modern, cloud optimized extensible web applications. All the new MVC features are described in detail and the author explains how best to apply them to both new and existing projects. The ASP.NET Core MVC Framework is the latest evolution of Microsoft’s ASP.NET web platform, built on a completely new foundation. It represents a fundamental change to how Microsoft constructs and deploys web frameworks and is free of the legacy of earlier technologies such as Web Forms. ASP.NET Core MVC provides a "host agnostic" framework and a high-productivity programming model that promotes cleaner code architecture, test-driven development, and powerful extensibility. Best-selling author Adam Freeman has thoroughly revised this market-leading book and explains how to get the most from ASP.NET Core MVC. He starts with the nuts-and-bolts and shows you everything through to advanced features, going in-depth to give you the knowledge you need. This book follows the same format and style as the popular previous editions but brings everything up to date for the new ASP.NET Core MVC release. It presents a fully worked case study of a functioning ASP.NET MVC application that readers can use as a template for their own projects. What You Will Learn: Gain a solid architectural understanding of ASP.NET Core MVC Explore the entire ASP.NET MVC Framework as a cohesive whole See how MVC and test-driven development work in action Learn what's new in ASP.NET Core MVC and how best to apply these new featu

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值