Interview questions C# - Habilya/LearningCourseNotes GitHub Wiki

In c# what is the difference between static and readonly variable?

Answer:

Static variable in C# is a variable that belongs to the 
class itself rather than to any instance of the class. 
It is shared among all instances of the class. 
The value of a static variable remains the same across all instances of the class.

Readonly variable in C# is a variable whose value can only be set at 
the time of declaration or in the class's constructor. 
Once initialized, the value of a readonly variable cannot be changed. 
Readonly variables are instance-specific and do not change across instances of the class.

In summary, the main differences between static and readonly variables are:

Scope: Static variables are shared among all instances of the class, 
while readonly variables are specific to each instance of the class.
Value assignment: Static variables can be assigned a new value at any time, 
while readonly variables can only be assigned a value at the time of 
declaration or in the constructor and cannot be changed afterward.

What are the different versions of .net (different version types .net standard, .net framework, .net core)?

  1. .NET Framework: The .NET Framework is a software framework developed by Microsoft that runs primarily on Microsoft Windows. It includes a large class library known as Framework Class Library (FCL) and provides language interoperability across several programming languages.

  2. .NET Core: .NET Core is an open-source, cross-platform version of the .NET Framework that runs on Windows, macOS, and Linux. It is modular and lightweight, allowing developers to include only the necessary components for their applications, making it suitable for microservices and containerized deployments.

  3. .NET Standard: .NET Standard is a set of APIs that are available across all .NET implementations, including .NET Framework, .NET Core, and Xamarin. It provides a common base for developing libraries that can be used in any .NET platform, ensuring compatibility between different versions of .NET.

These versions of .NET offer developers flexibility and choice in developing applications for various platforms and environments.

What is the difference between index and clustered index in databases?

Answer:

An index in a database is a data structure that improves the speed of 
data retrieval operations on a table. 
There are two main types of indexes: 
clustered indexes 
and non-clustered indexes.

Clustered Index:
A clustered index determines the physical order of data in a table.
There can only be one clustered index per table, as it organizes the table's data rows.
By default, the primary key of a table is often used as the clustered index, but it is not mandatory.
Data rows are stored on disk in the same order as the clustered index, which helps in fast retrieval of data.
A clustered index is particularly useful for tables with range queries and queries that return a range of rows.


Non-Clustered Index:
A non-clustered index is a separate data structure that stores copies 
of columns or expressions from a table to improve query performance.
Multiple non-clustered indexes can be created on a table.
Non-clustered indexes do not affect the physical order of data in a table.
They are useful for columns that are frequently used in SELECT, JOIN, and WHERE clauses.
Non-clustered indexes require additional storage space and 
memory overhead but help in improving query performance.
In summary, the main difference between clustered and 
non-clustered indexes is in how data is physically ordered and stored on disk. 
Clustered indexes directly affect the physical order of data, 
while non-clustered indexes are separate data structures that improve 
query performance without changing the physical order of data.

How to evaluate a sql query performance in mssql?

There are several ways to evaluate the performance of a SQL query in Microsoft SQL Server (MSSQL). Some of the methods include:

  1. Query Execution Plan: Use the "Display Estimated Execution Plan" option in SQL Server Management Studio to view the execution plan of the query. This will show how the query is being processed by the SQL Server engine and identify any potential performance bottlenecks.

  2. SQL Profiler: Use SQL Profiler to monitor and analyze the performance of the query in real-time. This tool helps you capture and analyze the query execution process, identify slow-performing queries, and troubleshoot performance issues.

  3. Database Tuning Advisor: Use the Database Tuning Advisor tool in SQL Server Management Studio to analyze the query workload and suggest improvements to enhance query performance. This tool provides recommendations for creating or modifying indexes, statistics, and other objects to optimize query performance.

  4. Performance Monitor: Use Performance Monitor to monitor the performance of the SQL Server instance, including CPU usage, memory usage, disk I/O, and other key performance metrics. This will help you identify any resource bottlenecks that may be affecting query performance.

  5. Execution Statistics: Use the SET STATISTICS TIME and SET STATISTICS IO options in SQL Server Management Studio to display the execution time and I/O statistics of the query. This will help you understand how long the query took to execute and how many I/O operations were performed.

By using these methods, you can effectively evaluate the performance of a SQL query in Microsoft SQL Server and make necessary adjustments to optimize query performance.

Why stringbuilder is better that string concatenation on very large string sets?

Answer:

StringBuilder is better than string concatenation on very large string sets 
because string concatenation creates a new string object each time you append a string to it, 
resulting in a lot of memory allocations and deallocations. 
This can be very inefficient and slow, especially when dealing with a large number of strings.

On the other hand, StringBuilder is designed to efficiently handle string concatenation 
by internally managing a mutable buffer to which you can append strings. 
This means that StringBuilder uses much less memory and is faster than string concatenation when dealing with large string sets.

Additionally, StringBuilder provides methods for manipulating and modifying strings, 
such as inserting, removing, or replacing characters, 
which can be very handy when working with large strings.

What are the disadvantages of dependency injection c#?

Answer:

Complexity: Dependency injection can introduce additional complexity to the codebase, 
especially for beginners or developers unfamiliar with the concept. 
This can make the code harder to understand and maintain.


Performance overhead: Depending on the implementation, using dependency injection in 
C# can introduce performance overhead compared to direct object instantiation.
This can be a concern in performance-critical applications.


Runtime errors: In some cases, misconfiguring dependencies or not handling dependencies 
correctly can lead to runtime errors that may be difficult to debug and fix.


Boilerplate code: Dependency injection often requires writing additional code to 
configure and manage dependencies, 
which can lead to boilerplate code that clutters the codebase.


Learning curve: Dependency injection can have a steep learning curve, 
especially for developers who are new to the concept. 
This can make it challenging to onboard new team members or work on existing codebases 
that heavily rely on dependency injection.

Git what is the difference between merge no fast forward and rebase?

When merging in git, 
there are two main strategies to combine changes from one branch into another: 
merge (no fast forward) 
and rebase.


Merge (no fast forward):
With this strategy, when merging changes from one branch into another, 
git creates a new merge commit to combine the changes.
This strategy maintains the history of the branches and shows 
a clear separation of where the merging occurred.
It is recommended to use merge (no fast forward) when working on 
a shared branch or when you want to preserve the history of the changes.

Rebase:
Rebasing is the process of moving the commits of one branch on top of another branch, 
effectively rewriting the commit history.
When rebasing, git applies each commit of the rebased branch 
on top of the target branch, creating a linear history without any merge commits.
This strategy makes the commit history cleaner and easier to understand, 
but it can also introduce conflicts that need to be resolved.
It is recommended to use rebase when working on 
a feature branch that will eventually be merged into the main branch, 
as it helps in maintaining a cleaner commit history.
In summary, merge (no fast forward) creates a new merge commit when 
combining changes, while rebase moves the commits of one branch on top of another branch. 
The choice between merge and rebase depends 
on the specific use case and preferences for the commit history.