net developer interview question and answer
net developer interview question and answer
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
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.
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
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.
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.
Concurrent collections are supported in the .NET Framework 4 with the System.Collections.Concurrent namespace. They help with
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.
ArrayList
Stack
Queue
Hashtable
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
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
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
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.
Nếu có nhiều cấp độ hơn chúng ta có thể sử dụng câu lệnh sau:
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:
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();
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