C# 面试题(中英文)
中文:
- 传入某个属性的set方法的隐含参数的名称是什么?
value,它的类型和属性所声名的类型相同。 - 如何在C#中实现继承?
在类名后加上一个冒号,再加上基类的名称。 - C#支持多重继承么?
不支持。可以用接口来实现。 - 被protected修饰的属性/方法在何处可以访问?
在继承或间接继承与这个类的子类中可以访问。 - 私有成员会被继承么?
会,但是不能被访问。所以看上去他们似乎是不能被继承的,但实际上确实被继承了。 - 请描述一下修饰符protected internal。
被protected internal修饰的属性/方法只能在它的在同一个程序集(Assembly)中的子类被访问。 - C#提供一个默认的无参数构造函数,当我实现了另外一个有一个参数的构造函数时候,还想保留这个无参数的构造函数。这样我应该写几个构造函数?
两个,一旦你实现了一个构造函数,C#就不会再提供默认的构造函数了,所以需要手动实现那个无参数构造函数。 - C#中所有对象共同的基类是什么?
System.Object. - 重载和覆写有什么区别?
重载提供了对一个方法签名的不同参数调用的实现。覆写提供了子类中改变父类方法行为的实现。 - 在方法定义中,virtual有什么含意?
被virtual修饰的方法可以被子类覆写。 - 能够将非静态的方法覆写成静态方法么?
不能,覆写方法的签名必须与被覆写方法的签名保持一致,除了将virtual改为override。 - 可以覆写私有的虚方法么?
不可以,甚至子类中无法访问父类中的私有方法。 - 能够阻止某一个类被其他类继承么?
可以,使用关键字sealed。 - 能够实现允许某个类被继承,但不允许其中的某个方法被覆写么?
可以,标记这个类为public,并标记这个方法为sealed。 - 什么是抽象类(abstract class)?
一种不可以被实例化的类。抽象类中一般含有抽象方法,当然也可有具体实现。继承类只有实现过所有抽象类的抽象方法后才能被实例化。 - 何时必须声明一个类为抽象类?
当这个类中包含抽象方法时,或是该类并没有完全实现父类的抽象方法时。 - 接口(interface)是什么?
只含有共有抽象方法(public abstract method)的类。这些方法必须在子类中被实现。 - 为什么不能指定接口中方法的修饰符?
接口中的方法用来定义对象之间通信的契约,指定接口中的方法为私有或保护没有意义。他们默认为公有方法。 - 可以继承多个接口么?
当然。 - 那么如果这些接口中有重复的方法名称呢?
这种情况中你可以决定如何实现。当然需要特别得小心。但是在编译环节是没有问题的。 - 接口和抽象类的区别是什么?
接口中所有方法必须是抽象的,并且不能指定方法的访问修饰符。抽象类中可以有方法的实现,也可以指定方法的访问修饰符。 - 如何区别重载方法?
不同的参数类型,不同的参数个数,不同的参数顺序。 - const和readonly有什么区别?
const关键字用来声明编译时常量,readonly用来声明运行时常量。 - System.String 和System.StringBuilder有什么区别?
System.String是不可变的字符串。System.StringBuilder存放了一个可变的字符串,并提供一些对这个字符串修改的方法。
英文:
- What is the implicit name of the parameter passed into a property’s ’set’ method?
‘value’. And it’s data type depends on whatever variable we’re changing. - How do you inherit from a class in C#? Place a colon and then the name of the base class.
- Does C# support multiple inheritances? No, use interfaces instead.
- When you inherit a protected class-level variable, who is it available to? Only classes that ultimately inherit from the class with a protected member can see that member.
- Are private class-level variables inherited? Yes, but they are not accessible, so looking at it you can honestly say that they are not inherited. But they are.
- Describe the accessibility modifier protected internal. It’s available to derived classes and classes within the same Assembly (and naturally from the base class it’s declared in).
- C# provides a default constructor for me. I write a constructor that takes a string as a parameter, but want to keep the no parameter one. How many constructors should I write? Two. Once you write at least one constructor, C# cancels the freebie constructor, and now you have to write one yourself, even if there’s no implementation in it.
- What’s the top .NET class that everything is derived from? System.Object.
- How’s method overriding different from overloading? When overriding, you change the method behavior for a derived class. Overloading simply involves having a method with the same name within the class.
- What does the keyword virtual mean in the method definition? The method can be over-ridden.
- Can you declare the override method static while the original method is non-static? No, you can’t, the signature of the virtual method must remain the same, only the keyword virtual is changed to keyword override.
- Can you override private virtual methods? No, moreover, you cannot access private methods in inherited classes, have to be protected in the base class to allow any sort of access.
- Can you prevent your class from being inherited and becoming a base class for some other classes? Yes, that’s what keyword sealed in the class definition is for. The developer trying to derive from your class will get a message: cannot inherit from Sealed class WhateverBaseClassName. It’s the same concept as final class in Java.
- Can you allow class to be inherited, but prevent the method from being over-ridden? Yes, just leave the class public and make the method sealed.
- What’s an abstract class? A class that cannot be instantiated. A concept in C++ known as pure virtual method. A class that must be inherited and have the methods over-ridden. Although an abstract class does not require implementations (its methods can be abstract) it can also offer implementations of methods (either virtual or not) which can be called in implementing classes.
- When do you absolutely have to declare a class as abstract (as opposed to free-willed educated choice or decision based on UML diagram)? When at least one of the methods in the class is abstract. When the class itself is inherited from an abstract class, but not all base abstract methods have been over-ridden.
- What’s an interface class? It’s an abstract class with public abstract methods all of which must be implemented in the inherited classes.
- Why can’t you specify the accessibility modifier for methods inside the interface? They all must be public. Therefore, to prevent you from getting the false impression that you have any freedom of choice, you are not allowed to specify any accessibility, it’s public by default.
- Can you inherit multiple interfaces? Yes, why not.
- And if they have conflicting method names? It’s up to you to implement the method inside your own class, so implementation is left entirely up to you. This might cause a problem on a higher-level scale if similarly named methods from different interfaces expect different data, but as far as compiler cares you’re okay.
- What’s the difference between an interface and abstract class? In the interface all methods must be abstract. In the abstract class some methods can be concrete. In the interface no accessibility modifiers are allowed, which is ok in abstract classes.
- How can you overload a method? Different parameter data types, different number of parameters, different order of parameters.
- What is the difference between const and readonly?
The const keyword is used for compile time constants while the readonly keyword is used for runtime constants. - What’s the difference between System.String and System.StringBuilder classes? System.String is immutable while System.StringBuilder was designed with the purpose of having a mutable string where a variety of operations can be performed.