0% found this document useful (0 votes)
16 views

net developer interview question and answer

question and answer for .net developers

Uploaded by

Son Dang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

net developer interview question and answer

question and answer for .net developers

Uploaded by

Son Dang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Framework excel trong .net bạn sử dụng là gì.?

The framework excel I often use is EPPlus package


2. What is Entity Framework
Entity Framework is an open-source ORM (Object Relational Mapping) Framework
for the .NET applications supported by Microsoft. Entity framework is something
which specify the relational mapping of object. Entity framework provides mechanism to
store and access data in the database automatically. It is a very import concept of MVC
model
3. What is the .Net
.NET is an open-source platform for building desktop, web, and mobile applications that
can run natively on any operating system. The .NET system includes tools, libraries,
and languages that support modern, scalable, and high-performance software
development. An active developer community maintains and supports the .NET
platform.

In simple terms, the .NET platform is a software that can do these tasks:

 Translate .NET programming language code into instructions that a computing device
can process.
 Provide utilities for efficient software development. For example, it can find the current
time or print text on the screen.
 Define a set of data types to store information like text, numbers, and dates on the
computer

4. What is the .Net framework

The .NET is a Framework, which is a collection of classes of reusable libraries given by


Microsoft to be used in other .NET applications and to develop, build and deploy many
types of applications on the Windows platform including the following:

 Console Applications
 Windows Forms Applications
 Windows Presentation Foundation (WPF) Applications
 Web Applications
 Web Services
 Windows Services
 Services-oriented applications using Windows Communications Foundation
(WCF)
 Workflow-enabled applications using Windows Workflow Foundation(WF)
5. Lazing loading trong entityframework

Lazy Loading is a Process where Entity Framework loads the related entities on
demand. Lazy Loading is the default behavior of Entity Framework. That means the
related entities or child entities are loaded only when it is being accessed for the first
time. That means in simple words we can say that Lazy loading simply delays the
loading of related entities until you specifically request it

The one and only difference is loading the related entities. In Eager loading, all the
related entities are loaded while loading the Main entity and on the other hand, with
Lazy Loading, the related entities are going to be loaded on demand i.e. when required
or when accessing the navigation property of the Main entity at that time only the related
entities are going to be loaded from the database.

When to Use Eager Loading and Lazy Loading in Entity Framework?


So, it completely depends on the business requirement of your application whether to
use Lazy Loading or Eager Loading.
If you are not interested in related entities or the related entities are not used instantly,
then you can use Lazy Loading to improve the application performance. In this case, if
you go with Eager loading, then it will load the related entities instantly even though they
are not required or are not required instantly. The reason is Eager Loading will generate
and execute SQL Join whereas Lazy Loading simply uses the SELECT Statement
without any join. So, definitely, in this case, Lazy Loading will give a better performance
than Eager Loading.
If you are interested in related entities or the related entities are used instantly in your
application, then you need to go with Eager Loading to get better performance. In this
case, if you go with Lazy Loading, then it will not load the related entities instantly and it
will load the related entities by issuing separate SQL queries. That means if the main
entity has 100 records, then for each record i.e. 100 times it will issue a separate SQL
Query to get the child entity data. Then you can imagine, how many round trips between
the .NET Application and the Database. With Eager loading, while loading the Main
entity the child entities are loaded. That means using a Single Round Trip i.e. using a
single SQL Statement with Join will load the related entities. As the number of round
trips is decreased, so the performance will be improved.

Key differences between DELETE and TRUNCATE

The following points explain the differences between delete and truncate command:

1. The DELETE statement is used when we want to remove some or all of the
records from the table, while the TRUNCATE statement will delete entire rows
from a table.
2. DELETE is a DML command as it only modifies the table data, whereas the
TRUNCATE is a DDL command.
3. DELETE command can filter the record/tuples by using the WHERE clause.
However, the TRUNCATE command does not allow to use WHERE clause, so
we cannot filter rows while truncating.
4. DELETE activates all delete triggers on the table to fire. However, no triggers
are fired on the truncate operation because it does not operate on individual
rows.
5. DELETE performs deletion row-by-row one at a time from the table, in the order,
they were processed. However, TRUNCATE operates on data pages instead of
rows because it deleted entire table data at a time.
6. DELETE statement only deletes records and does not reset the table's identity,
whereas TRUNCATE resets the identity of a particular table.
7. DELETE command require more locks and database resources because it
acquires the lock on every deleted row. In contrast, TRUNCATE acquires the
lock on the data page before deleting the data page; thus, it requires fewer locks
and few resources.
8. DELETE statement makes an entry in the transaction log for each deleted row
whereas, TRUNCATE records the transaction log for each data page.
9. TRUNCATE command is faster than the DELETE command as it deallocates the
data pages instead of rows and records data pages instead of rows in transaction
logs.
10. Once the record deletes by using the TRUNCATE command, we cannot recover
it back. In contrast, we can recover the deleted data back which we removed
from the DELETE operation.

Collections in C#

Types of collections

C# collections are categorized into 3 main namespaces:


System.Collections.Generic classes (Generic)

System.Collections.Concurrent classes (Concurrent)

System.Collections classes (Non-Generic)

To add elements to a collection, we declare an instance of a class.

For example, when we are using elements of the same data type, we declare
System.Collections.Generic namespace which will import all the required classes.

System.Collections.Generic classes (Generic)

In case the elements are of the same data type, we use one of the classes in the
System.Collections.Generic namespace. The generic collection only accepts one data
type and no other data type.

The classes provided by the Generic collections include:


List
Stack
Queue
LinkedList
HashSet
SortedSet
Dictionary
SortedDictionary
SortedList

System.Collections.Concurrent classes (Concurrent)

Concurrent collections are supported in the .NET Framework 4 with the System.Collections.Concurrent namespace. They help with

access to collection objects from several threads.

If many threads need to access a collection at the same time, the concurrent collections
should be used instead of non-generic and generic collections.
The classes provided by the concurrent collections include:

BlockingCollection
ConcurrentBag
ConcurrentStack
ConcurrentQueue
ConcurrentDictionary
Partitioner
OrderablePartitioner
System.Collections classes (Non-Generic)

Unlike the generic collection, non-generic collections accept elements of different data types and utilize the System.Collections
namespace.

The classes provided by the Non-Generic collections include:

ArrayList
Stack
Queue
Hashtable

Let’s look at each one of them:

1 ArrayList
It represents ordered collection of an object that can be indexed individually.

It is basically an alternative to an array. However, unlike array you can add and remove items from a list at a specified position using an

index and the array resizes itself automatically. It also allows dynamic memory allocation, adding, searching and sorting items in the list.

2 Hashtable
It uses a key to access the elements in the collection.

A hash table is used when you need to access elements by using key, and you can identify a useful key value. Each item in the hash

table has a key/value pair. The key is used to access the items in the collection.

3 SortedList
It uses a key as well as an index to access the items in a list.

A sorted list is a combination of an array and a hash table. It contains a list of items that can be accessed using a key or an index.

If you access items using an index, it is an ArrayList, and if you access items using a key , it is a Hashtable. The collection of items

is always sorted by the key value.

4 Stack
It represents a last-in, first out collection of object.

It is used when you need a last-in, first-out access of items. When you add an item in the list, it is called pushing the item and when
you remove it, it is called popping the item.

5 Queue
It represents a first-in, first out collection of object.

It is used when you need a first-in, first-out access of items. When you add an item in the list, it is called enqueue and when you

remove an item, it is called deque.

6 BitArray
It represents an array of the binary representation using the values 1 and 0.

It is used when you need to store the bits but do not know the number of bits in advance. You can access items from the

BitArray collection by using an integer index, which starts from zero.

Bài viết này chúng ta sẽ tìm hiểu về Lazyloading, Earger Loading và Explicit Loading trong Entity
Framework mà chúng ta thường xuyên gặp trong thực tế. Ở 3 khái niệm này, chúng ta đều tham
chiếu đến việc load các entity liên quan.

Eager Loading

Eager loading giúp bạn load tất cả các entity trong 1 câu lệnh, tất cả các entity con sẽ được load ra
trong 1 lần gọi duy nhất. Điều này có thể được thực hiện thông qua phương thức Include, sẽ trả về
các entity liên quan như một phần của câu query và một lượng lớn dữ liệu sẽ được load ra 1 lần.

Ví dụ, bạn có một bản User và một bảng UserDetails (bảng này liên kết đến bảng User), sau đó bạn
sẽ viết code như dưới đây. Chúng ta sẽ load ra user với Id của nó bằng UserId ở bảng UserDetails.

User usr = dbContext.Users.Include(a => a.UserDetails).FirstOrDefault(a => a.


UserId == userId);

Nếu có nhiều cấp độ hơn chúng ta có thể sử dụng câu lệnh sau:

User usr = dbContext.Users.Include(a => a.UserDetails.Select(ud => ud.Address


)).FirstOrDefault(a => a.UserId == userId);

Lazy Loading

Đây là một hành vi mặc định của Entity Framework, khi mà các entity con được load ra chỉ khi
chúng được truy cập lần đầu tiên. Đơn giản là hoãn lại việc load các dữ liệu ở các enttiy liên quan
cho đến khi bạn yêu cầu nó.

Ví dụ, khi chúng ta chạy câu lện bên dưới, bảng UserDetails sẽ không được tải ra theo bảng User:

User usr = dbContext.Users.FirstOrDefault(a => a.UserId == userId);

Nó sẽ chỉ được load ra khi bạn công khai gọi nó:

UserDeatils ud = usr.UserDetails; // UserDetails are loaded here

Xét thêm 1 ví dụ nữa:

public class Person{


public String Name{get; set;}
public String Email {get; set;}
public virtual Employer employer {get; set;}
}

public List<EF.Person> GetPerson(){


using(EF.DbEntities db = new EF.DbEntities()){
return db.Person.ToList();
}
}

Sau khi phương thức GetPerson() được gọi, bạn không thể dùng lazyloading để truy cập vào thuộc
tính employer để lấy ra đối tượng Employer khi ra ngoài phương thức nữa. Vì sao? Vì đối tượng db
đã bị hủy sau khi ra ngoài khỏi khối using. Vây bạn phải dùng Person.Include(x=>x.employer) để
load sẵn dữ liệu của Employer vào thuộc tính của person trước khi đối tượng db bị hủy.

Explicit Loading

Có nhiều tùy chọn để disable tính năng Lazyloading trong Entity Framework. Sau khi tắt Lazy
Loading, bạn có thể vẫn load các enttiy liên quan bằng cách gọi tường minh phương thức Load() để
tải các entity liên quan. Có 2 cách sử dụng phương thức Load, Reference và Collection (để load
collections):
User usr = dbContext.Users.FirstOrDefault(a => a.UserId == userId);
dbContext.Entry(usr).Reference(usr => usr.UserDetails).Load();

Khi nào sử dụng cái gì:

1. Sử dụng Eager loading khi các mối quan hệ không quá nhiều. Vì thế
Eager loading là cách tối để giảm lượng query đến server vì chỉ có 1
query duy nhất.
2. Sử dụng Eager loading khi bạn chắc chắn bạn sẽ sử dụng các entity
liên quan với entity chính ở nhiều nơi và nhiều lần.
3. Sử dụng Lazyloading khi bạn sử dụng qua nhệ 1-N
4. Sử dụng Lazyloading khi bạn chắc chắn bạn không sử dụng các
entity liên quan ngay lập tức.
5. Khi bạn tắt tính năng Lazyloading, sử dụng Explicit loading khi bạn
không chắc rằng bạn sẽ có sử dụng entity trước đó hay không

You might also like