2019-11-19-C#-高级面试题
| title | author | date | CreateTime | categories |
|---|---|---|---|---|
|
C# 高级面试题
|
lindexi
|
2019-11-19 08:40:50 +0800
|
2018-11-12 11:18:2 +0800
|
C#
|
很少会有人可以答对,如果你遇到一个来面试的人实在嚣张,就可以用本文的题去打击
本文内容就看着玩,请不要在严肃的面试中问题这样的题目
如果面试到一个人可以回答出下面的题目也不能证明他的技术很强,只能说明他了解很多C#相关,或者他看过我的博客
循环下面的代码
请在下面的代码的注释处填写代码,让函数 Foo 里面的代码输出
static void Main(string[] args)
{
// 请在此处写代码,调用 Foo 函数内的输出代码
} private static void Foo()
{
try
{
while (true)
{
}
}
finally
{
Console.WriteLine("尝试调用 Foo 函数执行这一句代码");
}
}
参考答案
使用一个线程调用的方式,调用之后结束线程,此时就会输出
static void Main(string[] args)
{
// 请在此处写代码,调用 Foo 函数内的输出代码 var thread = new Thread(Foo);
thread.Start();
Task.Delay(100).Wait();
thread.Abort();// 这时就会结束循环 Console.Read();
}
注意,在 dotnet core 不支持 Abort 方法
从空转换
请写出 IFoo 和 Foo 的实现,让下面的代码不会抛出空异常
static void Main(string[] args)
{
Foo foo = (IFoo) null;
foo.Name = "lindexi"; Console.Read();
}
参考答案
class IFoo
{ } class Foo
{
public string Name { get; set; } public static implicit operator Foo(IFoo foo)
{
return new Foo();
}
}
等待不存在的类
请添加新的类的代码让下面的代码编译通过
class Program
{
static async Task Main(string[] args)
{
Foo foo = await (object) null;
foo.Name = "lindexi"; Console.Read();
}
} public class Foo
{
public string Name { get; set; }
}
参考答案
public class HeabdsdnbKevx : INotifyCompletion
{
public bool IsCompleted { get; } public Foo GetResult()
{
return new Foo();
} /// <inheritdoc />
public void OnCompleted(Action continuation)
{
}
} public static class RelelnisSou
{
public static HeabdsdnbKevx GetAwaiter(this object obj)
{
return new HeabdsdnbKevx();
}
}
再高级一点,写出下面的代码
static async Task Main(string[] args)
{
await await await await await await await await await await await await
await await await await await await await "林德熙是逗比";
}
其实很简单,也就是将 GetResult 修改一下,在上面的代码修改
public string GetResult()
{
return "林德熙是逗比";
}
因为返回值是 string 所以又可以继续等待
如何不执行 finally 里面的代码
这里有一个代码,需要让 finally 里面的代码不执行,现在你只能写 Foo 方法,同时这个方法不能运行无限长时间
try
{
Foo();
}
finally
{
Console.WriteLine("不要让这个代码运行");
}
参考答案
因为不能让 Foo 运行无限长,就不能使用无限循环的方法,可以使用的方法有 Environment.FailFast 或 Environment.Exit 退出
private static void Foo()
{
Environment.Exit(0);
}
或者进行堆栈溢出,如下面代码
private static void Foo()
{
Foo();
}
或者 少珺 小伙伴的不安全代码申请
private static void Foo()
{
unsafe
{
var n = stackalloc int[int.MaxValue];
}
}
或者干掉自己进程
private static void Foo()
{
Process.GetCurrentProcess().Kill();
}
但是申请大内存和退出当前线程方法都会让 finally 执行
private static void Foo()
{
var n = new int[int.MaxValue];
}
// 虽然提示内存不够,但是finally依然可以运行
退出当前线程抛出的是线程中断异常,和其他异常一样都能执行 finally 代码
private static void Foo()
{
Thread.CurrentThread.Abort();
}
注意,在 dotnet core 不支持 Abort 方法
另外,如果进入 try 是不能使用 goto 跳出但不执行 finally 代码
如果是在 VisualStudio 调试,在 Foo 执行完之后,在 VS 里把调试箭头拖到 finally 的后面
请问下面代码输出多少
请问下面的代码的 n 的值是多少?
class Foo
{
public int N { get; } = 1;
} Foo foo = null;
var n = 2 + foo?.N ?? 1; Console.WriteLine(n);
参考答案
1
可能有小伙伴认为在 2 + foo?.N 这时如果 foo 为空就应该返回 ?? 后面的值,但是这是不对的上面的代码是和下面的代码差不多等同的
if (foo == null)
{
n = 1;
}
else
{
n = 2 + foo.N;
}
而不是和下面的代码等价的
if (foo == null)
{
n = 2 + 1;
}
else
{
n = 2 + foo.N;
}
在表达里面只有 ? 的值为空,那么就不会执行
等等,为什么上面的代码说的是差不多等同而不是等价,因为尝试运行下面代码,会看到 Hi 输出,多谢 九鼎 指出
using System;
class Test
{
class Foo
{
public int N
{
get
{
Console.WriteLine("Hi.");
return 1;
}
}
} static void Main()
{
Foo foo = null;
Foo foo2 = new Foo();
var n = 2 + foo?.N + foo2.N ?? 1;
Console.WriteLine(n);
}
}
上面代码中,第一个 foo?.N 会进行判断,因为 foo 不存在,所以整个表达式没有执行,但是表达式内的逻辑依然执行
模式匹配
请问下面代码输出什么?
class B
{
public static int operator &(B left, B right) => 1;
public static int operator >(B left, B right) => 2;
public static int operator <(B left, B right) => 3; public static int operator &(bool left, B right) => 5;
public static int operator >(bool left, B right) => 6;
public static int operator <(bool left, B right) => 7;
} private static B B { get; } static void Main(string[] args)
{
object a = null;
B c = null;
Console.WriteLine(a is B b & c);
Console.WriteLine(a is B b1 > c);
Console.WriteLine(a is B b2 < c); a = new B(); Console.WriteLine(a is B b5 & c);
Console.WriteLine(a is B b6 > c);
Console.WriteLine(a is B b7 < c); }
也许这是全部题目里面最简单的一道题
请看 C# 匹配可空变量
其实这里的 a is B 用的 B 是 class 不是定义的属性,对 a is B b5 返回的是 bool 所以将会是 bool 与 B 之间的运算
2019-11-19-C#-高级面试题的更多相关文章
- cisco ssh实验--附带配置脚本-2019.11.19
cisco ssh实验
- Java高级面试题解析(一)
最近,在看一些java高级面试题,我发现我在认真研究一个面试题的时候,我自己的收获是很大的,我们在看看面试题的时候,不仅仅要看这个问题本身,还要看这个问题的衍生问题,一个问题有些时候可能是一个问题群( ...
- 2019前端面试系列——JS面试题
判断 js 类型的方式 1. typeof 可以判断出'string','number','boolean','undefined','symbol' 但判断 typeof(null) 时值为 'ob ...
- 解析“60k”大佬的19道C#面试题(下)
解析"60k"大佬的19道C#面试题(下) 在上篇中,我解析了前 10 道题目,本篇我将尝试解析后面剩下的所有题目. 姐妹篇:解析"60k"大佬的19道C#面试 ...
- 【读书笔记】2016.11.19 北航 《GDG 谷歌开发者大会》整理
2016.11.19 周六,我们在 北航参加了<GDG 谷歌开发者大会>,在web专场,聆听了谷歌公司的与会专家的技术分享. 中午免费的午餐,下午精美的下午茶,还有精湛的技术,都是我们队谷 ...
- php高级面试题知识点(转载)
php高级面试题知识点大全 时间:2016-01-26 06:36:22来源:网络 导读:php高级面试题知识点大全,本套面试题内容包括php魔术方法.php单点登录.linux基本命令.前端开发技术 ...
- Spring 配置文件详解 http://www.blogjava.net/hellxoul/archive/2011/11/19/364324.html
1.基本配置: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http: ...
- 2019.3.18考试&2019.3.19考试&2019.3.21考试
2019.3.18 C O D E T1 树上直接贪心,环上for一遍贪心 哇说的简单,码了将近一下午终于码出来了 感觉自己码力/写题策略太糟糕了,先是搞了一个细节太多的写法最后不得不弃疗了,然后第二 ...
- How to Pronounce Numbers 11 – 19
How to Pronounce Numbers 11 – 19 Share Tweet Share Tagged With: Numbers Numbers are something you’ll ...
随机推荐
- 使用JS如何消除一个数组里重复的元素
JS: var arrData = [1,3,5,7,7,8,9,3,10,8,"sdsdsds","sss","ffff","s ...
- 公司mysql问题二
这个问题解决:
- c++编译错误:invalid new-expression of abstract class type
原因: 出现这个错误原因是new 了一个抽象类出错,说明父类(接口)中有纯虚函数没有实现.接口里的纯虚函数全部需要实现,这样才能new 子类. 例如: 纯虚函数例如 ; 是纯虚函数,不是纯虚函数不作要 ...
- ZOJ 3956 Course Selection System [01背包]
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3956 题意:就是给你Hi,Ci的值,问怎么取使得下面那个式子的值最大: 理 ...
- iPhone 7 Plus 维修记 (一)(2019-08-07)
iPhone 7 Plus 维修记 问题 电池鼓包,屏幕已经被撑起,偶尔死机突然关机. 分析 初步分析是电池损坏. 维修 由于电池没有双易拉条需要将后壳加热后再取出电池. 更换电池后测试,发现电量一会 ...
- 快速删除项目中的输出日志console.log
项目开发时,控制台往往有许多忘记删除或注释掉的输出日志.但是上线后总不能一个一个删吧,最近总结出几个解决思路 重写console.log方法,使其失去 输出能力 这个最直接有效,用vue框架的话放在m ...
- 【Leetcode链表】环形链表 II(142)
题目 给定一个链表,返回链表开始入环的第一个节点. 如果链表无环,则返回 null. 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始). 如果 pos ...
- dataframe添加元素指定为列表,不同for循环命名空间下的变量重复问题
split=pd.DataFrame({'data':[0],'len':0,'count':0},index=[0])for i_t in range(over_128.shape[0]): ct= ...
- @NOIP2018 - D1T2@ 货币系统
目录 @题目描述@ @题解@ @代码@ @题目描述@ 在网友的国度中共有 n 种不同面额的货币,第 i 种货币的面额为 a[i],你可以假设每一种货币都有无穷多张.为了方便,我们把货币种数为 n.面额 ...
- Android系列之Android 命令行手动编译打包详解
Android 命令行手动编译打包过程图 [详细步骤]: 1使用aapt生成R.java类文件: 例: E:\androidDev\android-sdk-windows2.2\tools> ...