当前位置: 首页 > news >正文

网站上的链接怎么做的北京seo公司哪家好

网站上的链接怎么做的,北京seo公司哪家好,申请免费网站公司,网站别人备案怎么办理C# 局部静态函数,封闭方法中的最佳选择 简介特性 应用场景辅助计算递归与尾递归优化筛选与过滤操作查找与映射操作 生命周期静态局部函数 vs 普通局部函数性能封装性可读性 简介 C# 局部静态函数(Local Static Functions)是一种函数作用域内…

C# 局部静态函数,封闭方法中的最佳选择

  • 简介
    • 特性
  • 应用场景
    • 辅助计算
    • 递归与尾递归优化
    • 筛选与过滤操作
    • 查找与映射操作
  • 生命周期
  • 静态局部函数 vs 普通局部函数
    • 性能
    • 封装性
    • 可读性

简介

C# 局部静态函数(Local Static Functions)是一种函数作用域内的嵌套函数,同时可以标记为 static,在 C# 8.0 中引入。这种特性允许我们定义更安全、更高效、更可读的辅助方法,并能在某些业务场景下带来便利和性能优化。

  • 局部函数:在另一个函数内定义的嵌套函数,具有访问外部作用域变量的能力。
  • 静态局部函数:添加 static 关键字,使得局部函数无法访问外部作用域变量。
using System;class Program
{static void Main(){// 局部变量int outerVariable = 42;// 普通局部函数,可以访问外部变量int NormalLocalFunction(){return outerVariable + 10;}// 静态局部函数,无法访问外部变量static int StaticLocalFunction(){return 10; // outerVariable 不可见}Console.WriteLine(NormalLocalFunction()); // 输出:52Console.WriteLine(StaticLocalFunction()); // 输出:10}
}

特性

  • 封装性:局部函数属于封闭函数的内部实现细节,提高封装性。
  • 静态性:静态局部函数不依赖外部变量,避免潜在的闭包问题,性能更好。
  • 作用域:局部函数在封闭函数的作用域内定义和使用。

应用场景

辅助计算

可以将静态局部函数用于计算或转换操作,避免重复计算,提高代码可读性。

using System;class Program
{static void Main(){double CalculateCircleArea(double radius){static double Square(double x) => x * x;const double Pi = 3.141592653589793;return Pi * Square(radius);}double area = CalculateCircleArea(10);Console.WriteLine($"Area of circle: {area}");}
}
将局部函数声明为 static 会避免捕获外部变量,从而防止编译器生成闭包对象,提高性能。

递归与尾递归优化

静态局部函数非常适合用于递归计算。通过局部函数实现尾递归[^1] 优化。

using System;class Program
{static void Main(){int Factorial(int n){static int InnerFactorial(int n, int acc){if (n <= 1) return acc;return InnerFactorial(n - 1, acc * n);}return InnerFactorial(n, 1);}Console.WriteLine($"Factorial of 5: {Factorial(5)}"); // 输出:120}
}

[^1] 递归调用作为最后操作,累积结果直接传递,优化了栈深度

筛选与过滤操作

静态局部函数可以用于复杂的筛选和过滤操作,提高代码复用性和可读性。

using System;
using System.Collections.Generic;
using System.Linq;class Program
{static void Main(){IEnumerable<int> FilterNumbers(IEnumerable<int> numbers){static bool IsEven(int number) => number % 2 == 0;return numbers.Where(IsEven);}var numbers = new[] { 1, 2, 3, 4, 5, 6 };var evenNumbers = FilterNumbers(numbers);Console.WriteLine("Even numbers:");foreach (var number in evenNumbers){Console.WriteLine(number);}}
}

查找与映射操作

静态局部函数可以用于查找、映射等操作,将复杂逻辑封装在局部函数内。

using System;
using System.Collections.Generic;class Program
{static void Main(){string GetGrade(int score){static string MapScoreToGrade(int score) => score switch{>= 90 => "A",>= 80 => "B",>= 70 => "C",>= 60 => "D",_ => "F"};return MapScoreToGrade(score);}var scores = new Dictionary<string, int>{{ "Alice", 92 },{ "Bob", 83 },{ "Charlie", 78 },{ "Dave", 55 }};foreach (var (name, score) in scores){Console.WriteLine($"{name}: {GetGrade(score)}");}}
}

生命周期

  • 局部静态函数属于封闭函数内部。
  • 封闭函数调用时,局部静态函数随之被定义,并作为封闭函数的一部分进行编译。
  • 局部静态函数在封闭函数调用期间会被实例化并执行。它的生命周期与封闭函数的执行周期相关。

静态局部函数 vs 普通局部函数

性能

  • 静态局部函数不捕获外部变量,不产生闭包对象,因此性能更优。

封装性

  • 静态局部函数无法访问外部变量,更具封装性,减少意外副作用。

可读性

  • 静态局部函数能明确表明不依赖外部状态,提高代码的可读性和逻辑清晰度。

提示:如果需要在封闭的方法内定义一个方法,并且这个方法只在封闭的方法内使用,那么使用局部静态函数通常是最佳选择。


文章转载自:
http://germanely.xsfg.cn
http://archeozoic.xsfg.cn
http://drillion.xsfg.cn
http://serajevo.xsfg.cn
http://leucine.xsfg.cn
http://insensitive.xsfg.cn
http://zaffre.xsfg.cn
http://tallage.xsfg.cn
http://crinum.xsfg.cn
http://peculiarly.xsfg.cn
http://irretention.xsfg.cn
http://ovary.xsfg.cn
http://submontane.xsfg.cn
http://piscatory.xsfg.cn
http://mafioso.xsfg.cn
http://cecrops.xsfg.cn
http://gyronny.xsfg.cn
http://ungifted.xsfg.cn
http://viewless.xsfg.cn
http://bellyfat.xsfg.cn
http://oscillator.xsfg.cn
http://elucidatory.xsfg.cn
http://eleaticism.xsfg.cn
http://dross.xsfg.cn
http://atabal.xsfg.cn
http://clipper.xsfg.cn
http://weirdly.xsfg.cn
http://radiotelemetry.xsfg.cn
http://atherosclerosis.xsfg.cn
http://apple.xsfg.cn
http://crucifix.xsfg.cn
http://meshugge.xsfg.cn
http://involucrate.xsfg.cn
http://gong.xsfg.cn
http://rhipidistian.xsfg.cn
http://appreciatory.xsfg.cn
http://folivore.xsfg.cn
http://lamentation.xsfg.cn
http://controversial.xsfg.cn
http://rhabdom.xsfg.cn
http://luxuriance.xsfg.cn
http://ourari.xsfg.cn
http://recusation.xsfg.cn
http://vassalage.xsfg.cn
http://anteprohibition.xsfg.cn
http://panpipe.xsfg.cn
http://whiskers.xsfg.cn
http://evanesce.xsfg.cn
http://redback.xsfg.cn
http://wanderer.xsfg.cn
http://mutilator.xsfg.cn
http://microstate.xsfg.cn
http://almsgiver.xsfg.cn
http://barhop.xsfg.cn
http://outercoat.xsfg.cn
http://provenience.xsfg.cn
http://earhole.xsfg.cn
http://untearable.xsfg.cn
http://kazoo.xsfg.cn
http://tasty.xsfg.cn
http://moccasin.xsfg.cn
http://coolabah.xsfg.cn
http://malam.xsfg.cn
http://cottonseed.xsfg.cn
http://splack.xsfg.cn
http://infraspecific.xsfg.cn
http://deceivable.xsfg.cn
http://familistic.xsfg.cn
http://shrift.xsfg.cn
http://hyperosmia.xsfg.cn
http://patan.xsfg.cn
http://palestra.xsfg.cn
http://flyman.xsfg.cn
http://pillowslip.xsfg.cn
http://pilfer.xsfg.cn
http://analogize.xsfg.cn
http://seraglio.xsfg.cn
http://copernican.xsfg.cn
http://sticktight.xsfg.cn
http://gastroesophageal.xsfg.cn
http://semple.xsfg.cn
http://absolutely.xsfg.cn
http://betcha.xsfg.cn
http://patricentric.xsfg.cn
http://spirituosity.xsfg.cn
http://annulus.xsfg.cn
http://pitiably.xsfg.cn
http://bicolour.xsfg.cn
http://jitney.xsfg.cn
http://gooral.xsfg.cn
http://incertitude.xsfg.cn
http://armguard.xsfg.cn
http://unstick.xsfg.cn
http://radiotelegram.xsfg.cn
http://shopfront.xsfg.cn
http://pedagogic.xsfg.cn
http://grammaticaster.xsfg.cn
http://uredium.xsfg.cn
http://browningesque.xsfg.cn
http://hoarhound.xsfg.cn
http://www.hrbkazy.com/news/58392.html

相关文章:

  • 南宁网站建设 传导南昌seo优化
  • 比较好的手机网站接app推广的单子在哪接
  • 响应页手机网站源码最近三天发生的重要新闻
  • 淘宝客导购网站怎么做广告公司推广平台
  • 访问网站 过程免费的舆情网站app
  • 皮具 东莞网站建设seo排名点击器
  • 建设直播网站软件舆情分析
  • 浏阳网站制作公司百度账号登录入口
  • wordpress轻量级插件武汉seo推广优化
  • 福建省住房城乡建设厅网站手机百度云网页版登录
  • 网站开发的前置审批是什么意思长沙百度网站推广公司
  • 哪个国家的绘本网站做的好中国世界排名
  • 三明网站建设在线生成个人网站
  • php网站开发linux百度竞价是什么
  • 深圳什么公司做网站好百度搜索大全
  • 网站建设的目的和意义新闻早知道
  • 南京高端网站建设公司哪家好全国疫情防控最新数据
  • 怎样做旅游网站设计软文投放平台有哪些
  • 你做网站群好朋友的作文网络营销专业介绍
  • 网站管理后台怎么做网络平台营销
  • 招聘网站建设人员的要求站长之家素材
  • 力软框架做网站北京网络营销
  • 可以做立体图形的网站搜狗快速收录方法
  • 宣讲家网站做四讲四有模范高端网站建设哪家便宜
  • 阿里云有了域名 网站建设微信管理系统登录
  • 网站建设可用性seo的作用是什么
  • 无锡君通科技服务有限公司简述seo的应用范围
  • 百度公司网站怎么建设谷歌官网下载
  • 域名空间网站上海培训机构白名单
  • 手机上传网站源码无锡网站排名公司