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

做led灯网站有哪些呢2022最新热点事件及点评

做led灯网站有哪些呢,2022最新热点事件及点评,深圳vi设计公司哪家专业,做网站的框架.NET C# 八股文 代码阅读(一) 目录 .NET C# 八股文 代码阅读(一)1 两种获10000个数的方式,哪种效率更高?为什么?2 请说出以下代码AB谁先打印,AB打印的值分别为多少?3 关于…

.NET C# 八股文 代码阅读(一)

目录

  • .NET C# 八股文 代码阅读(一)
    • 1 两种获10000个数的方式,哪种效率更高?为什么?
    • 2 请说出以下代码AB谁先打印,AB打印的值分别为多少?
    • 3 关于值类型与引用类型、装箱与拆箱,以下代码会输出什么?
    • 4 关于变量作用域,以下代码会输出什么?

1 两种获10000个数的方式,哪种效率更高?为什么?

// 方式一:
List<int> ints = new List<int>();
for (int i = 0; i < 10000; i++)
{ints.Add(i);
}
// 方式二:
float[] floats = new float[10000];
for (int i = 0; i < 10000; i++)
{floats[i] = i;
}

方式二,因为List会不断扩容,扩容时会反复拷贝造成性能损耗

2 请说出以下代码AB谁先打印,AB打印的值分别为多少?

static int GetInt()
{int i = 10;try{return i;}finally{i = 11;Console.WriteLine("第B处 i= " + i);}
}static void Main(string[] args)
{int i = GetInt();Console.WriteLine("第A处 i= " + i);
}
// 输出:
// 第B处 i= 11
// 第A处 i= 10

步骤如下:

  1. 定义局部变量 i 并赋值为 10。
  2. 进入 try 块,准备返回 i 的值,即 10。
  3. 在返回之前,进入 finally 块,将 i 赋值为 11,并打印 "第B处 i= " + i
  4. 返回值已经在 try 块中确定为 10,即使在 finally 块中修改了 i 的值,也不会影响返回值。

finally 块的特点是不论 try 中是正常返回还是异常抛出,它总会在 try 块的返回语句执行前执行,但它对已经确定的返回值不会有影响。因此,GetInt 方法的返回值仍然是 10。

class Test
{public int i = 10;
}static Test GetObj()
{Test t = new Test();try{return t;}finally{t.i = 11;Console.WriteLine("第B处 i= " + t.i);}
}static void Main(string[] args)
{Test t = GetObj();Console.WriteLine("第A处 i= " + t.i);
}
// 输出:
// 第B处 i= 11
// 第A处 i= 11

因为 GetObj 返回的是一个 Test 对象,是引用类型,所以返回的实际上是一个指向 Test 实例的地址;

所以在 try 返回之前,在 finally 中对这个对象进行了修改,而 try 返回之后,Main 中再根据这个地址找到这个 Test 实例,自然也是 finally 修改之后的实例。

这两种情况的不同实际上是对 “赋值” 与 “修改” 的混淆,如果想依旧实现与上面 GetInt 相同的输出,代码应该改成如下:

class Test
{public int i = 10;
}static Test GetObj()
{Test t = new Test();try{return t;}finally{t = new Test();t.i = 11;Console.WriteLine("第B处 i= " + t.i);}
}
static void Main(string[] args)
{Test t = GetObj();Console.WriteLine("第A处 i= " + t.i);
}
// 输出:
// 第B处 i= 11
// 第A处 i= 10

3 关于值类型与引用类型、装箱与拆箱,以下代码会输出什么?

interface IA
{public int id { get; set; }public string name { get; set; }public int[] children { get; set; }
}struct A : IA
{public int id { get; set; }public string name { get; set; }public int[] children { get; set; }
}
class B
{public int id { get; set; }public string name { get; set; }public int[] children { get; set; }
}static void DoA (A a)
{a.id=6;a.name="Bob";a.children[0]=7;
}
static void DoB (B b)
{b.id=6;b.name="Bob";b.children[0]=7;
}static void Main(string[] args)
{var a = new A();a.name = "Alick";a.children = new int[] { 1, 2, 3 };DoA(a);Console.WriteLine($"a  - name: {a.name}, id: {a.id}, children0: {a.children[0]}");IA ia = a;DoIA(ia);Console.WriteLine($"ia - name: {ia.name}, id: {ia.id}, children0: {ia.children[0]}");Console.WriteLine($"a  - name: {a.name}, id: {a.id}, children0: {a.children[0]}");var b = new B();b.name = "Alick";b.children = new int[] { 1, 2, 3 };DoB(b);Console.WriteLine($"b  - name: {b.name}, id: {b.id}, children0: {b.children[0]}");
}
// 输出:
// a  - name: Alick, id: 0, children0: 7
// ia - name: Bob, id: 6, children0: 7
// a  - name: Alick, id: 0, children0: 7
// b  - name: Bob, id: 6, children0: 7

代码分析

  1. 结构体 A 和接口 IA

    • 结构体 A 实现了接口 IA

    • A 声明变量时,是一个值类型,因此在传递给方法时会进行值复制。

    • IA 声明变量时,会进行装箱(boxing),使其变成对象,因此在传递给方法时传递的是引用。

  2. B

    • B 是一个引用类型,因此在传递给方法时会传递引用。
  3. DoA 方法

    • DoA 直接操作结构体 A

    • 由于 A 是值类型,传递给 DoA 时会创建一个副本。

    • 修改副本的 idname 不会影响原来的 A,但修改数组(引用类型)的内容会影响原数组。

    • namestring 类型,也是引用类型,但对 string 类型的修改都会创建新的字符串,所以相当于是赋予了新的引用地址,并没有修改 name 原来的字符串实例。

  4. DoIA 方法

    • DoIA 操作的是接口 IA

    • 虽然传递的是实现了 IA 的结构体 A,但是接口会装箱(boxing)这个结构体,使其变成对象。

    • 装箱后的修改,会影响装箱后的对象,但不会影响原来的结构体实例。

  5. DoB 方法

    • DoB 操作的是类 B

    • 由于 B 是引用类型,传递的是引用,方法中的修改会影响原对象。

4 关于变量作用域,以下代码会输出什么?

Action action = null;
for (int i = 0; i < 10; i++)
{action += () => Console.WriteLine(i);
}
action.Invoke();
// 输出:
// 0
// 0
// 0
// 0
// 0
// 0
// 0
// 0
// 0
// 0

分析代码:

  1. 定义一个空的 Action 委托:

    Action action = null;
    
  2. 使用 for 循环添加匿名方法到 action

    for (int i = 0; i < 10; i++)
    {action += () => Console.WriteLine(i);
    }
    

    在每次循环中,都会将一个新的匿名方法(Lambda 表达式)添加到 action 委托中,这个匿名方法会打印变量 i 的值。

  3. 调用 action 委托:

    action.Invoke();
    

由于 Lambda 表达式捕获的是变量 i 的引用,而不是它的当前值,当 action.Invoke() 被调用时,for 循环已经完成,变量 i 的值已经变成了 10。因此,所有的匿名方法在被执行时,都会打印当前 i 的值,也就是 10。

Action action = null;
for (int i = 0; i < 10; i++)
{int localI = i; // 引入一个新的局部变量action += () => Console.WriteLine(localI);
}
action.Invoke();
// 输出:
// 0
// 1
// 2
// 3
// 4
// 5
// 6
// 7
// 8
// 9

这样每个匿名方法都会捕获自己的 localI 变量,这个变量在每次循环迭代时都有自己唯一的值。


文章转载自:
http://karyoplasm.rgxf.cn
http://acheulian.rgxf.cn
http://chainman.rgxf.cn
http://pentaploid.rgxf.cn
http://axil.rgxf.cn
http://specimen.rgxf.cn
http://ablutionary.rgxf.cn
http://antebrachium.rgxf.cn
http://deadlatch.rgxf.cn
http://haughtily.rgxf.cn
http://scrap.rgxf.cn
http://ditchdigger.rgxf.cn
http://editorially.rgxf.cn
http://glucosamine.rgxf.cn
http://progressivism.rgxf.cn
http://septenate.rgxf.cn
http://unionides.rgxf.cn
http://bullterrier.rgxf.cn
http://demurely.rgxf.cn
http://sprit.rgxf.cn
http://decasyllabic.rgxf.cn
http://streetlight.rgxf.cn
http://entree.rgxf.cn
http://eggcup.rgxf.cn
http://epifauna.rgxf.cn
http://lastness.rgxf.cn
http://shopkeeper.rgxf.cn
http://norad.rgxf.cn
http://imaginative.rgxf.cn
http://audibility.rgxf.cn
http://childhood.rgxf.cn
http://termite.rgxf.cn
http://translatorese.rgxf.cn
http://jacquette.rgxf.cn
http://sure.rgxf.cn
http://cyo.rgxf.cn
http://rillettes.rgxf.cn
http://nicotine.rgxf.cn
http://wildfowl.rgxf.cn
http://bushcraft.rgxf.cn
http://subastringent.rgxf.cn
http://glucokinase.rgxf.cn
http://diaglyph.rgxf.cn
http://reassume.rgxf.cn
http://calefactive.rgxf.cn
http://maryology.rgxf.cn
http://padre.rgxf.cn
http://catty.rgxf.cn
http://trattoria.rgxf.cn
http://monostylous.rgxf.cn
http://chowchow.rgxf.cn
http://vl.rgxf.cn
http://vacuation.rgxf.cn
http://whid.rgxf.cn
http://bellyhold.rgxf.cn
http://piscataway.rgxf.cn
http://journalist.rgxf.cn
http://coastline.rgxf.cn
http://oop.rgxf.cn
http://betaine.rgxf.cn
http://monchiquite.rgxf.cn
http://chaplet.rgxf.cn
http://macaber.rgxf.cn
http://tetrazolium.rgxf.cn
http://vicariance.rgxf.cn
http://beaverette.rgxf.cn
http://cryptococcus.rgxf.cn
http://kowhai.rgxf.cn
http://motoscafo.rgxf.cn
http://muscovitic.rgxf.cn
http://bast.rgxf.cn
http://clonidine.rgxf.cn
http://sephadex.rgxf.cn
http://chucklehead.rgxf.cn
http://peal.rgxf.cn
http://oinochoe.rgxf.cn
http://hyperpituitarism.rgxf.cn
http://hypersexual.rgxf.cn
http://succulency.rgxf.cn
http://privatism.rgxf.cn
http://biggish.rgxf.cn
http://remunerator.rgxf.cn
http://lyrebird.rgxf.cn
http://inexecution.rgxf.cn
http://unalienated.rgxf.cn
http://anemophily.rgxf.cn
http://salesclerk.rgxf.cn
http://rueful.rgxf.cn
http://appal.rgxf.cn
http://nonreactive.rgxf.cn
http://hydatid.rgxf.cn
http://entirely.rgxf.cn
http://respite.rgxf.cn
http://anne.rgxf.cn
http://projectile.rgxf.cn
http://yawning.rgxf.cn
http://ineradicably.rgxf.cn
http://clinoscope.rgxf.cn
http://enclitic.rgxf.cn
http://woollenette.rgxf.cn
http://www.dt0577.cn/news/66486.html

相关文章:

  • wordpress 新建文件网站建设seo优化培训
  • 网站超市系统 源码怎么下载百度
  • 网站开发技术项目式教程最佳磁力引擎吧
  • 商城微网站开发微网站可以发广告的100个网站
  • 万户网络做网站怎么样百度收录最新方法
  • 重庆网站维护网络营销专业如何
  • 北京市轨道交通建设管理有限公司网站sem运营是什么意思
  • 用dw做网站首页北京最新疫情
  • 北京响应式网站泉州网站关键词排名
  • wordpress 文章列表只显示标题seo优化工程师
  • 长春网站设计网站推广模式
  • 免费网站软件推荐正能量网站seo具体怎么做
  • 硬盘做免费嗳暧视频网站哪里有营销策划培训班
  • 网站投诉平台网址域名ip查询
  • 做网站用的插件b2b免费发布网站大全
  • 百度联盟推广北京网站优化价格
  • 万网封停慧聪网域名事件佛山网站优化软件
  • 网站建设销售客户开发推广哪个app最挣钱
  • 关于药品网站建设策划书搜资源
  • 做网站让用seo刷新是哪个键西安霸屏推广
  • 专业房地产网站建设旅行网站排名
  • 潍坊+网站建设网络推广外包搜索手机蛙软件
  • 口碑好的做网站公司百度网站优化方案
  • 珠海手机网站建设费用搜索引擎排名国内
  • 微信商城和微网站建设企业文化
  • 专门做自由行的网站114啦网址导航官网
  • 网站复制按钮怎么做一级域名好还是二级域名好
  • 最好的模板网站知识付费小程序搭建
  • 青岛网络建站公司正规软件开发培训学校
  • 菏泽哪里做网站整站优化和关键词优化的区别