.NET basics Flashcards
Abstract class vs interface
An abstract class allows you to create functionality that subclasses can implement or override.
An interface only allows you to define functionality, not implement it.
string vs. System.String
In C#, the string keyword is an alias for String; therefore, String and string are equivalent.
It’s recommended to use the provided alias string as it works even without using System;
Are Strings Immutable in c#
String objects are immutable: they can’t be changed after they’ve been created.
All of the String methods and C# operators that appear to modify a string actually return the results in a new string object.
Advantages of Using Interface
Interface allow us to develop loosely couples systems , dependency injection and make unit testing and mocking easier
When do you use abstract class?
Generally for base controllers or base classes where we don’t want an object of it to be created. So we use abstract class
What is entity framework?
Entity Framework is an open-source ORM framework for .NET applications supported by Microsoft.
It enables developers to work with data using objects of domain-specific classes without focusing on the underlying database tables and columns where this data is stored.
What are the features of Entity Framework?
Querying: EF allows us to use LINQ queries (C#/VB.NET) to retrieve data from the underlying database. The database provider will translate these LINQ queries to the database-specific query language (e.g. SQL for a relational database). EF also allows us to execute raw SQL queries directly to the database.
Caching: EF includes the first level of caching out of the box. So, repeated querying will return data from the cache instead of hitting the database.
What is context class in Entity Framework?
The context class is a most important class while working with EF 6 or EF Core.
It represent a session with the underlying database using which you can perform CRUD (Create, Read, Update, Delete) operations.
How does Entity Framework work?
Entity Framework API includes the ability to map domain (entity) classes to the database schema, translate and execute LINQ queries to SQL, track changes occurred to the entities during lifetime, and save changes to the database
How to create a Custom Context class in EF?
By inheriting DbContext class
How to register a Custom Context class in EF?
Register the Custom Context class in the ConfigureServices of the startup.cs file
What is the role of ConfigureServices() ?
It takes care of registering the services consumed across the application using Dependency Injection
What is the role of Configure() ?
It is used to add Middleware components to the IApplicationBuilder instance available in the method itself.
Using this method we can build middleware such as routing , authentication and session as well as third party middleware
what is wwwrootfolder in .NET core?
It contains static files such as HTML , CSS and JavaScript and only these files inside the wwwrootfolder can be served over HTTP requests
What is garbage collection?
A low priority process that serves as an automatic memory manager which manages the allocation and release of memory for applications
What is delegate?
A delegate in .NET is similar to a function pointer in C/C++.
Disadvantages of Query String
All the attributes and values are visible to the end user. Therefore, they are not secure. - There is a limit to URL length of 255 characters.
What are PUT, POST and PATCH ?
POST is used for creating a resource ( does not matter if it was duplicated )
PUT is for checking if a resource exists then update, else create new resource(Body contains whole entity to be updated)
PATCH is always for updating a resource(Body may contain only required fields)
How to make public APIs more secure ?
By using Authentication and Authorization well , we can secure the APIs.
ViewBag vs ViewData
ViewData is a dictionary of objects that is derived from ViewDataDictionary class and accessible using strings as keys.
ViewBag is a dynamic property that takes advantage of the new dynamic features in C# 4.0.
ViewData requires typecasting for complex data type and check for null values to avoid error.
ViewBag doesn’t require typecasting for complex data type.
How to configure custom routing in .NET mvc?
Inside Configure () method of Startup.cs , use app.UseMVC() method that takes two parameters.
For example ,
app.UseMvc(routes=>{routes.MapRoute(“default”,”{controller=Home}/{action=Index}/{id?}”)})
What is Attribute routing ?
Attribute Routing enables us to define routing on top of the controller action method.
For example,
public class HomeController: Controller
{
//URL: /Mvctest
[Route(“Mvctest”)] //Mvctest corresponds to absolute URL
public ActionResult Index()
ViewBag.Message = “Welcome to ASP.NET MVC!”;
return View();
}}
We can also set a common prefix for the entire controller (all action methods within the controller) using the “RoutePrefix” attribute.
How to do Authorization in .Net MVC?
Authorization can be done by using [authorize] attribute on top of controller action method or inside configureServices of startup.cs
what does [AllowAnonymous] do?
This allows anonymous requests to be processed for those that don’t need any authorization . This works exact opposite of [Authorize] attribute
Class vs struct
Classes Only:
Can support inheritance
Are reference (pointer) types
The reference can be null
Have memory overhead per new instance
Structs Only:
Cannot support inheritance
Are value types
Are passed by value (like integers)
Cannot have a null reference (unless Nullable is used)
Do not have a memory overhead per new instance - unless ‘boxed’
What are common between class and struct ?
Are compound data types typically used to contain a few variables that have some logical relationship
Can contain methods and events
Can support interfaces
When to choose Struct over the class?
CONSIDER a struct instead of a class:
If instances of the type are small and commonly short-lived or are commonly embedded in other objects.
AVOID a struct unless the type has all of the following characteristics:
It logically represents a single value, similar to primitive types (int, double, etc.).
It has an instance size under 16 bytes.
It is immutable. (cannot be changed)
It will not have to be boxed frequently.
Does ASP.NET session uses cookies ? If so , what are cookies
Yes , ASP.NET used cookies
Cookies : cookies store small amount of information on the client’s machine.Websites use cookies to store user preferences or other information .
Persistent cookies : Even after browser is closed , they live.
Non persistent cookies : Get destroyed once browser is closed
What is garbage collection?
garbage collector (GC) serves as an automatic memory manager. The garbage collector manages the allocation and release of memory(managed heap) for an application.
To optimize the performance of the garbage collector, the managed heap is divided into three generations, 0, 1, and 2, so it can handle long-lived and short-lived objects separately.
For most of the objects your application creates, you can rely on garbage collection to perform the necessary memory management tasks automatically. However, unmanaged resources require explicit cleanup.
What are unmanaged objects that need manual garbage collection ?
The most common type of unmanaged resource is an object that wraps an operating system resource, such as a file handle, window handle, or network connection. Although the garbage collector can track the lifetime of a managed object that encapsulates an unmanaged resource, it doesn’t have specific knowledge about how to clean up the resource.
How to implement garbage collection on unmanaged objects
By Using IDisposable Interface and implementing
Dispose() and DisposeAsync() methods
Mention some .NET Performance Tips
It is best to avoid using value types in situations where they must be boxed a high number of times as it is costly operation converting value to reference as each time , object needs to be created.
Use StringBuilder instead of strings , as they create more temporary strings
Empty finalizers should not be used. When a class contains a finalizer, an entry is created in the Finalize queue. When the finalizer is called, the garbage collector is invoked to process the queue. If the finalizer is empty, this simply results in a loss of performance.