17 Essential .NET Interview Questions *

Toptal来源的基本问题认为最好 .NET developers and engineers can answer. 在我们社区的推动下,我们鼓励专家提交问题并提供反馈.

Hire a Top .NET Developer Now
Toptal logo是顶级自由软件开发人员的专属网络吗, designers, finance experts, product managers, and project managers in the world. 顶级公司雇佣Toptal自由职业者来完成他们最重要的项目.

Interview Questions

1.

解释什么是继承,为什么它很重要.

View answer

继承是面向对象程序设计中最重要的概念之一, together with encapsulation and polymorphism. 继承允许开发人员创建可重用的新类, extend, 并修改在其他类中定义的行为. 这样可以重用代码并加快开发速度. With inheritance, 开发人员只能编写和调试一个类一次, 然后重用相同的代码作为新类的基础. 其成员被继承的类称为基类, 继承这些成员的类称为派生类. By default, all classes in .NET are inheritable.

2.

解释类和对象之间的区别.

View answer

简而言之,类是对象的定义,对象是类的实例.

我们可以把类看作对象的模板:它描述了所有的属性, methods, 实现对象将具有的状态和行为. As mentioned, an object is an instance of a class, 类在实例化之前不会成为对象. 基于一个类可以有更多的对象实例,每个实例都有不同的属性.

3.

解释托管代码和非托管代码之间的区别.

View answer

Managed code is a code created by the .NET compiler. 它不依赖于目标机器的体系结构,因为它是由CLR(公共语言运行时)执行的。, and not by the operating system itself. CLR和托管代码给开发人员带来的好处很少, like garbage collection, type checking and exceptions handling.

On the other hand, 非托管代码直接编译为本机代码,并取决于目标机器的体系结构. It is executed directly by the operating system. In the unmanaged code, 开发人员必须确保他正在处理内存使用和分配(特别是由于内存泄漏)。, type safety and exceptions manually.

In .. NET、Visual Basic和c#编译器创建托管代码. 要获得非托管代码,应用程序必须用C或c++编写.

Apply to Join Toptal's Development Network

and enjoy reliable, steady, remote Freelance .NET Developer Jobs

Apply as a Freelancer
4.

Explain the difference between the while and for loop. Provide a .NET syntax for both loops.

View answer

当一个代码单元需要重复执行时,使用这两个循环. The difference is that the for 当您知道需要在代码中迭代多少次时,就可以使用循环. On the other hand, the while 当您需要重复某些内容直到给定语句为真时,使用循环.

The syntax of the while loop in C# is:

while (condition [is true])
{
  // statements
}

The syntax of the while loop in VB.NET is:

While condition [is True]
  ' statements
End While

The syntax of the for loop in C# is:

for (initializer; condition; iterator)
{
  // statements
}

The syntax of the for loop in VB.NET is:

For counter [As datatype] = start To end [Step Step]
  ' statements
Next [ counter ]
5.

解释装箱和拆箱之间的区别. Provide an example.

View answer

装箱是将值类型转换为类型对象的过程, 拆箱就是从对象中提取值类型. 装箱是隐式的,拆箱是显式的.

Example (written in C#):

int i = 13;
object myObject = i; 	// boxing 
i = (int)myObject;	// unboxing 
6.

Explain what LINQ is.

View answer

LINQ是语言集成查询(Language Integrated Query)的缩写,是在Visual Studio 2008中引入的. LINQ是一组将查询功能扩展到 .通过添加一组新的允许数据操作的标准查询操作符。NET语言语法, regardless of the data source. Supported data sources are: .. NET Framework集合,SQL Server数据库,ADO.. NET数据集、XML文档和任何支持 IEnumerable or the generic IEnumerable interface, in both C# and Visual Basic. 简而言之,LINQ在对象世界和数据世界之间架起了一座桥梁.

7.

讨论垃圾收集是什么以及它是如何工作的. 中强制垃圾收集的代码示例 .NET.

View answer

垃圾收集是一个低优先级的进程,它作为一个自动内存管理器,为应用程序管理内存的分配和释放. Each time a new object is created, 公共语言运行库从托管堆中为该对象分配内存. 只要托管堆中有可用的空闲内存空间, 运行时继续为新对象分配空间. However, memory is not infinite, 一旦应用程序填满了Heap内存空间, 垃圾收集可以释放一些内存. 当垃圾收集器执行收集时, 它检查托管堆中不再被应用程序使用的对象,并执行必要的操作来回收内存. Garbage collection will stop all running threads, 它会在堆中找到所有没有被主程序访问的对象并删除它们. 然后,它将重新组织堆中剩余的所有对象以腾出空间,并调整堆栈和堆中指向这些对象的所有指针.

手动在代码中强制垃圾收集, 你可以运行下面的命令(用c#写的):

System.GC.Collect();
8.

What do the following acronyms in .NET stand for: IL, CIL, MSIL, CLI and JIT?

View answer

IL,即中间语言,是一种独立于CPU的部分编译代码. 实时编译器(JIT)将使用当前环境属性将IL代码编译为本机机器码。. JIT编译器将IL代码转换为汇编代码,并使用目标机器的CPU架构来执行 .NET application. In .. NET中,IL被称为公共中间语言(CIL),在早期 .NET时代,它被称为微软中间语言(MSIL)。.

CLI,即公共语言基础结构,是微软开发的一个开放规范. 它是用于部署、版本控制和安全性的编译代码库. In .有两种CLI类型:进程程序集(EXE)和库程序集(DLL)。. CLI assemblies contain code in CIL, and as mentioned, during compilation of CLI programming languages, 源代码被翻译成CIL代码,而不是翻译成平台或处理器特定的目标代码.

To summarize:

  1. 编译时,源代码首先被翻译成IL (in) .NET, that is CIL, and previously called MSIL).
  2. 然后将CIL组装成字节码,并创建CLI程序集.
  3. Before code execution, CLI代码通过运行时的JIT编译器来生成本机机器码.
  4. 计算机的处理器执行本机机器码.
9.

解释堆栈和堆之间的区别.

View answer

简单的回答是:在Stack中存储值类型(继承自 System.ValueType),并在堆中存储引用类型(继承自 System.Object).

我们可以说Stack负责跟踪实际执行的内容以及每个执行线程的位置(每个线程都有自己的Stack)。. 另一方面,堆负责跟踪数据或更精确的对象.

10.

中接口和抽象类之间的区别 .NET.

View answer

An interface 仅声明实现类应该具有的契约或行为. 它只能声明没有访问修饰符的属性、方法和事件. All the declared members must be implemented.

An abstract class 为必须由继承实体实现的功能和一些抽象/虚拟成员提供部分实现. It can declare fields too.

接口和抽象类都不能被实例化.

11.

Explain deferred execution vs. immediate execution in LINQ. Provide examples.

View answer

In LINQ, deferred execution 这意味着在指定查询时没有执行查询. 具体来说,这是通过将查询赋值给一个变量来实现的. When this is done, 查询定义存储在变量中,但是在遍历查询变量之前不会执行查询. For example:

DataContext productContext = new DataContext();

var productQuery = from product in productContext . var.Products
        where product.Type == "SOAPS"
        select product;   // Query is NOT executed here

foreach (var product in productQuery) //查询在这里执行
{
  Console.WriteLine(product.Name);
}

You can also force immediate execution of a query. This can be useful, for example, if the database is being updated frequently, 在程序逻辑中,确保访问的结果是在代码中指定查询的地方返回的结果是很重要的. 的方法通常强制立即执行 Average, Sum, Count, List, ToList, or ToArray. For example:

DataContext productContext = new DataContext();

var productCountQuery = (from product in productContext ..Products
        where product.Type == "SOAPS"
        select product).Count();   // Query executes HERE
12.

What is a delegate in .NET?

View answer

A delegate in .. NET类似于C或c++中的函数指针. 使用委托允许程序员将对方法的引用封装在委托对象中. 然后可以将委托对象传递给可以调用引用方法的代码, 无需在编译时知道将调用哪个方法. 此外,我们可以使用委托在类中创建自定义事件. For example,

public delegate void FooDelegate();

class FooClass
{
    // custom event
    public event FooDelegate FooEvent;
}

FooClass FooObj = new FooClass()
FooObj.FooEvent += new FooDelegate();
13.

How do you implement a generic action in WebAPI?

View answer

这是不可能的,因为WebAPI运行时需要提前知道方法签名.

14.

为什么不能为接口中的项指定访问修饰符?

View answer

It is always public

15.

When break is used inside two nested for 循环,控制来自于哪个循环,内部的还是外部的 for loop? (I.e. does it break from all the present loops?)

View answer

It breaks from the inner loop only.

16.

You would know that System.Object is the parent class of all .NET classes; In other words all types in .. NET(无论是隐式的、显式的还是用户创建的)派生自 System.Object class.

What are the various methods provided to System.Object’s deriving classes/types?

View answer

System.对象提供了以下重要方法,其中包括:

  1. ToString-返回一个表示当前对象的字符串
  2. both overrides of Equals(object), Equals(object, object)
  3. GetHashCode
  4. Finalize
  5. GetType
  6. ReferenceEquals
  7. MemberwiseClone

这些方法中的大多数都提供了开发人员将使用的任何类型所需的基本实现 .NET stack.

17.

讨论常量和只读变量之间的区别.

View answer

而常量和只读变量有很多相似之处, there are some important differences:

  • 常量在编译时计算,而只读变量在运行时计算.
  • 常量只支持值类型的变量(唯一的例外是字符串), 而只读变量可以保存引用类型的变量.
  • 当值在运行时不改变时,应该使用常量, 只读变量主要用于它们的实际值在运行前未知的情况.
  • 只读变量只能在声明时或在构造函数中初始化.

面试不仅仅是棘手的技术问题, so these are intended merely as a guide. 并不是每一个值得雇佣的“A”候选人都能回答所有的问题, 回答所有问题也不能保证成为A级考生. At the end of the day, 招聘仍然是一门艺术,一门科学,需要大量的工作.

Why Toptal

Tired of interviewing candidates? Not sure what to ask to get you a top hire?

Let Toptal find the best people for you.

Hire a Top .NET Developer Now

Our Exclusive Network of .NET Developers

Looking to land a job as a .NET Developer?

Let Toptal find the right job for you.

Apply as a .NET Developer

Job Opportunities From Our Network

Submit an interview question

提交的问题和答案将被审查和编辑, and may or may not be selected for posting, at the sole discretion of Toptal, LLC.

* All fields are required

Looking for .NET Developers?

Looking for .NET Developers? Check out Toptal’s .NET developers.

Anne Adams

Freelance .NET Developer
United KingdomToptal Member Since September 4, 2015

Anne是一名经验丰富的开发人员,曾在大公司和初创公司工作过. 他在美林(Merrill Lynch)做了8年的工程师,开发了金融交易应用程序, Anne founded and built LoudUp, a music-based social network that she designed, developed, and launched from the ground up. She specializes in .NET technologies and JavaScript.

Show More

Marko Cirkvenčić

Freelance .NET Developer
CroatiaToptal Member Since November 19, 2019

Marko是一名IT专家,拥有信息学硕士学位和11年的软件开发经验 .NET and Microsoft SQL Server platforms. He's reliable, responsible, and an ambitious person, 随时准备掌握新的技能,取得新的成功. 他喜欢关注HTML5和JavaScript等新技术.

Show More

Laszlo Gyori

Freelance .NET Developer
HungaryToptal Member Since February 14, 2022

Laszlo是一位经验丰富的开发者和领导者,拥有15年的经验. 在软件开发和管理的各个阶段都有实践经验, he is a life-long learner, completionist, and problem solver. Laszlo的专长包括后台服务 .NET 6, databases using SQL and Oracle, front end on Angular 13, and cloud technologies such as AWS and Azure.

Show More

Toptal Connects the Top 3% of Freelance Talent All Over The World.

Join the Toptal community.

Learn more