zoukankan      html  css  js  c++  java
  • 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();
            }
    

    从空转换

    请写出 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);
    }
    

    请问下面代码输出多少

    请问下面的代码的 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;
                }
    

    在表达里面只有 ? 的值为空,那么就不会执行

    模式匹配

    请问下面代码输出什么?

    
        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 用的 Bclass 不是定义的属性,对 a is B b5 返回的是 bool 所以将会是 boolB 之间的运算

    我搭建了自己的博客 https://blog.lindexi.com/ 欢迎大家访问,里面有很多新的博客。只有在我看到博客写成熟之后才会放在csdn或博客园,但是一旦发布了就不再更新

    如果在博客看到有任何不懂的,欢迎交流,我搭建了 dotnet 职业技术学院 欢迎大家加入

    知识共享许可协议
    本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。欢迎转载、使用、重新发布,但务必保留文章署名林德熙(包含链接:http://blog.csdn.net/lindexi_gd ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请与我联系

  • 相关阅读:
    C++学习之【使用位操作符求素数分析】
    LeetCodeOJ刷题之13【Roman to Integer】
    QT学习之文件系统读写类
    让免费版MarkdownPad2使用Pro版本的功能
    QT学习之窗口右键菜单
    react 16.3+ 新生命周期 作业
    react 16.3+ 新生命周期
    node层设置proxy不生效的原因
    Javascript权威指南——读书笔记
    react踩坑
  • 原文地址:https://www.cnblogs.com/lindexi/p/12086913.html
Copyright © 2011-2022 走看看