C#面试题(曾经面试过三次)

来源:互联网 发布:fileinput.js 参数 编辑:程序博客网 时间:2024/05/15 02:06

1:求输出结果

 static void Main(string[] args)

        {
            int x = 20;

            int y = 40;

   GetResult(ref x, y);  

   Console.WriteLine("X:{0},Y:{1}", x, y);

        }

  static void GetResult(ref int x, int y)
        {
            x = x + y;
            y = x + y;
        }

 结果:X:60Y:40

我自己用Lambda表达式写了一下

 static void Main(string[] args)

        {
            int x = 20;

            int y = 40;

    Test_Void = (ref int X, int Y) => 

       {
                    X = X + Y;
                    Y = X + Y;
                };

   Console.WriteLine("X:{0},Y:{1}", x, y);

        }

 结果:X:60Y:40

个人见解:这个面试题主要考的是对ref参数的理解。

2:求输出的结果

class Program
 {

    static void Main(string[] args)
            {
                A aa = new B();
                aa.Fun();
                Console.Read();
            }

}

   public abstract class A
    {
        public A()
        {
            Console.WriteLine("A");
        }
        public virtual void Fun()
        {
            Console.WriteLine("A.Fun.()");
        }
    }
    public class B : A
    {
        public B()
        {
            Console.WriteLine("B");
        }
        public new void Fun()
        {
            Console.WriteLine("B.Fun.()");
        }
    }

结果:A   B    A,Fun();

个人见解:继承abstract抽象类的方法执行顺序

0 0
原创粉丝点击