Sunday, February 23, 2025

code

 How to reverse a string?

internal static void ReverseString(string str)  
{  
     
    char[] charArray = str.ToCharArray();  
    for (int i = 0, j = str.Length - 1; i < j; i++, j--)  
    {  
        charArray[i] = str[j];  
        charArray[j] = str[i];  
    }  
    string reversedstring = new string(charArray);  
    Console.WriteLine(reversedstring);  


    input: hello, output: olleh
input: hello world, output: dlrow olleh




Get Highest salary of 3 employee without using limit,and top SELECT StageID, DepartmentNumber, UserEmail ,DENSE_RANK() OVER (PARTITION BY DepartmentNumber ORDER BY UserEmail ASC) AS DRANK FROM mytable Get full name from First name and last name of table create view SELECT CONCAT (FirstName, ' , ' , LastName) AS fullname (salary*12)+(experience*500) AS total FROM staff var trans = transactions.OrderBy(t => t.PropertyName) .ThenBy(t => t.DifferentPropertyName) var trans = transactions.OrderByDescending(t => t.PropertyName) .ThenByDescending(t => t.DifferentPropertyName)

Thursday, February 20, 2025

.Net core

 



SQL:

Get Highest salary of 3 employee without using limit,and top

SELECT StageID, DepartmentNumber, UserEmail
        ,DENSE_RANK() OVER (PARTITION BY DepartmentNumber
         ORDER BY UserEmail ASC) AS DRANK 
   FROM mytable 


Get full name from First name and last name of table

create view

SELECT CONCAT (FirstName, ' , ' , LastName) AS fullname (salary*12)+(experience*500) AS total FROM staff


What is .NET Core, and how does it differ from the .NET Framework?

.NET Core (now simply .NET 5, 6, 7+) is a free, open-source, cross-platform framework for building modern applications that can run on Windows, macOS, and Linux.

The main differences are: 

• Cross-Platform: The .NET Framework is Windows-only, while .NET Core is cross-platform.

• Deployment: .NET Core applications can be deployed as self-contained executables that include all dependencies, whereas .NET Framework apps require the framework to be installed on the target machine.

• Modularity & Performance: .NET Core has a modular design and is optimized for performance and microservices architecture, making it suitable for cloud-native applications. 

2. Explain the different service lifetimes in ASP.NET Core Dependency Injection (DI).

Services in ASP.NET Core can be registered with three different lifetimes in the Startup.cs (or Program.cs in newer versions) file: 

• Transient: A new instance of the service is created every time it is requested from the service container. This is ideal for lightweight, stateless services.

• Scoped: An instance is created once per client request (connection) and is reused within that same request scope. It is disposed at the end of the request.

• Singleton: A single instance is created the first time it is requested and that same instance is used by all subsequent requests throughout the application's lifetime. 

3. What is middleware in ASP.NET Core, and how does it work?

Middleware components are software parts assembled into an application pipeline to handle HTTP requests and responses. Each component can choose to pass the request to the next component in the pipeline, perform logic before or after the next component, or "short-circuit" the pipeline entirely (e.g., in the case of an error or serving a static file). They are configured in the Configure method of the Startup class or directly in Program.cs. 

Architecture and Design Patterns

4. How do you implement Dependency Injection (DI) in a .NET Core application?

.NET Core has built-in support for DI. Services are registered in the ConfigureServices method of the Startup.cs file (or directly in the Program.cs using the IServiceCollection methods like AddSingleton, AddScoped, or AddTransient). The container then automatically injects these dependencies via the class constructor. 

5. Explain the role of Program.cs and Startup.cs files in a .NET Core project. 

• Program.cs: This is the entry point of the application, containing the Main method. It is responsible for creating and configuring the application host (which manages application lifetime, server configuration, logging, and DI).

• Startup.cs: This class (used in older versions, or implicitly configured in modern Program.cs via WebApplication.CreateBuilder) is where you configure the application's services (in ConfigureServices) and the HTTP request pipeline (in Configure using middleware). 

6. What is the CQRS (Command Query Responsibility Segregation) pattern, and how might you implement it in .NET Core?

CQRS is an architectural pattern that separates the operations for reading data (Queries) from those for updating data (Commands). 

• Implementation: In a .NET Core application, this often involves using different data models and potentially different databases for reads and writes. Libraries like MediatR are commonly used to route commands and queries to their respective handlers, promoting clear separation of concerns and scalability in complex, high-traffic applications. 

7. How do you handle transient faults (e.g., network glitches, temporary database unavailability) in .NET Core applications?

The Polly library is a popular choice for implementing resilience and transient fault handling in .NET Core. Polly allows you to define policies such as Retry, Circuit Breaker, and Timeout to handle temporary failures gracefully. These policies can be integrated with HttpClient for robust API calls. 

Advanced Topics

8. Explain the difference between synchronous and asynchronous programming in .NET Core, and when to use each.

• Synchronous: Code execution proceeds one line at a time. A process will block the main thread until it completes.

• Asynchronous: Uses keywords like async and await to perform operations (especially I/O-bound tasks like database calls or web service requests) without blocking the main thread, allowing the application to handle other requests simultaneously.

• When to use: Use synchronous programming for simple, CPU-bound operations. Use asynchronous programming for I/O-bound tasks in web applications to improve responsiveness, performance, and scalability by maximizing thread utilization. 

9. What are the benefits of using Entity Framework Core (EF Core) for data access?

EF Core is a lightweight, cross-platform Object-Relational Mapper (ORM). 

• Benefits:

• Allows developers to interact with the database using .NET objects, eliminating most manual SQL queries.

• Supports LINQ (Language Integrated Query) for data querying.

• Provides built-in features like change tracking, migrations, and support for in-memory databases for testing.

• Promotes a clean, object-oriented approach to data access. 

10. How do you secure Web APIs in ASP.NET Core using JWT (JSON Web Tokens)?

JWT authentication involves several steps: 

1. Configuration: Configure JWT middleware in the Startup.cs file, specifying token validation parameters (issuer, audience, signing key).

2. Login/Token Generation: When a user logs in, the application generates a JWT containing claims (user information) and signs it with a secret key.

3. Client Usage: The client stores the token (e.g., in local storage) and sends it in the Authorization header of subsequent requests.

4. Validation: The server-side middleware validates the token's signature and claims before allowing access to protected endpoints, often marked with the [Authorize] attribute. 


𝐌𝐚𝐬𝐭𝐞𝐫𝐢𝐧𝐠 .𝐍𝐄𝐓 𝐂𝐨𝐫𝐞: 20  𝐟𝐫𝐨𝐦 𝐄𝐚𝐬𝐲 𝐭𝐨 𝐇𝐚𝐫𝐝

What is .NET Core, and how does it differ from .NET Framework?
What is the role of the Startup.cs file in an ASP.NET Core application?

What are middleware components, and how do they work in the ASP.NET Core request pipeline?

Middleware is software that's assembled into an app pipeline to handle requests and responses. Each component:Chooses whether to pass the request to the next component in the pipeline.Can perform work before and after the next component in the pipeline.

How do you configure dependency injection in .NET Core?
What is the difference between IConfiguration and IOptions in .NET Core?
How does the appsettings.json file work in .NET Core?
What is the significance of the Program.cs file in .NET Core applications?

𝐈𝐧𝐭𝐞𝐫𝐦𝐞𝐝𝐢𝐚𝐭𝐞:
8. What is Kestrel, and why is it used in .NET Core?
9. Explain the concept of routing in ASP.NET Core.
10. What is the purpose of ConfigureServices and Configure methods in the Startup class?
11. How does ASP.NET Core handle cross-origin resource sharing (CORS)?
12. What are the benefits of using Entity Framework Core in .NET Core applications?
13. Explain the role of the IHostedService interface in .NET Core and provide an example of how to implement it.
14. How does ASP.NET Core support asynchronous programming?

𝐀𝐝𝐯𝐚𝐧𝐜𝐞𝐝

15. How does the ASP.NET Core request processing pipeline work internally?
16. How would you design and implement a microservices architecture using ASP.NET Core?
17. What is the difference between IApplicationBuilder and IServiceCollection in .NET Core?
18. How can you implement centralized logging and monitoring in a .NET Core application?
19. Explain the difference between transient, scoped, and singleton lifetimes in dependency injection.
20. How would you implement health checks in an ASP.NET Core application?
21. What is the purpose of the BackgroundService class, and how does it relate to IHostedService?
22. How does the .NET Core Generic Host differ from the Web Host?
23. How would you handle global exception handling in ASP.NET Core?
24. What is the purpose of DataProtection in .NET Core, and how would you use it?
25. How can you configure and manage multiple environments in ASP.NET Core?

JWT Token:

https://medium.com/@sajadshafi/jwt-authentication-in-c-net-core-7-web-api-b825b3aee11d




LinQ:

𝗟𝗜𝗡𝗤: 𝗘𝗦𝗦𝗘𝗡𝗧𝗜𝗔𝗟 𝗠𝗘𝗧𝗛𝗢𝗗𝗦 𝗙𝗢𝗥 𝗘𝗙𝗙𝗜𝗖𝗜𝗘𝗡𝗧 𝗤𝗨𝗘𝗥𝗜𝗘𝗦 𝗜𝗡 𝗖# 🚀

If you work with C#, mastering LINQ (Language Integrated Query) is a game-changer for writing cleaner and more efficient code. Here are some key LINQ methods you should know:

🔹 𝗪𝗵𝗲𝗿𝗲 → Filters elements based on a condition.
🔹 𝗦𝗲𝗹𝗲𝗰𝘁 → Projects or transforms elements into a new form.
🔹 𝗚𝗿𝗼𝘂𝗽𝗕𝘆 → Groups elements based on a specified key.
🔹 𝗢𝗿𝗱𝗲𝗿𝗕𝘆 → Sorts elements in ascending order.
🔹 𝗔𝗻𝘆 → Checks if any element satisfies a condition.
🔹 𝗔𝗹𝗹 → Verifies if all elements meet a condition.
🔹 𝗖𝗼𝘂𝗻𝘁 → Returns the number of elements.
🔹 𝗙𝗶𝗿𝘀𝘁 → Gets the first element (throws an exception if empty).
🔹 𝗙𝗶𝗿𝘀𝘁𝗢𝗿𝗗𝗲𝗳𝗮𝘂𝗹𝘁 → Gets the first element or a default value if empty.
🔹 𝗟𝗮𝘀𝘁 → Gets the last element (throws an exception if empty).
🔹 𝗗𝗶𝘀𝘁𝗶𝗻𝗰𝘁 → Removes duplicates from a sequence.
🔹 𝗧𝗮𝗸𝗲 → Retrieves a specified number of elements.
🔹 𝗦𝘂𝗺 / 𝗠𝗮𝘅 / 𝗠𝗶𝗻 / 𝗔𝘃𝗲𝗿𝗮𝗴𝗲 → Performs numeric calculations on a sequence.
🔹 𝗨𝗻𝗶𝗼𝗻 → Combines two sequences, returning unique elements.
🔹 𝗜𝗻𝘁𝗲𝗿𝘀𝗲𝗰𝘁 → Returns common elements between two sequences.
🔹 𝗘𝘅𝗰𝗲𝗽𝘁 → Returns elements in the first sequence that are not in the second.