Monday, 24 June 2024

What is static

  • A static class cannot contain any non-static members except const and enum.
  • A static class cannot inherit from other classes except the Object class. 
  • A static classes cannot implement interfaces. 
  • A static classes cannot be inherited because they are implicitly sealed by default. 
  • A static class cannot contain any instance constructors. 
  • A static class cannot be explicitly marked as sealed because it is already sealed by default. 
  • A static class can contain enums. explicit private constructors are not allowed in a static class. 
  • Only a static constructor (implicitly private) can be defined. 
  • A static class cannot be instantiated; it is implicitly abstract. 
  • A non-static class can contain static methods. 
  • A static method can be accessed using the class name itself, without creating an object of the class. 
  • A non-static class can also contain a static constructor. 
  • A static class cannot contain any destructor. an indexer cannot be defined in a static class because indexers cannot be static.
Share:

ASP.NET MVC request life cycle:

In ASP.NET MVC, understanding the request life cycle is essential for building robust web applications. Let’s explore the key stages of the ASP.NET MVC request life cycle: 
 
  Routing: 
  • When a client makes a request (e.g., visits a URL), the routing module processes it. 
  • The routing module maps the requested URL to a specific controller action based on predefined routes. 
  • Routes are defined in the RouteConfig.cs (or Startup.cs for ASP.NET Core) file. 
MVC Handler: 
  • Once the route is determined, the MVC handler is responsible for creating the appropriate controller instance. 
  • The controller is instantiated, and the action method is invoked. Controller Action Execution: 
  • The controller’s action method processes the request. 
  • It interacts with the model (data) and prepares the data to be displayed. 
  • The action method returns an ActionResult (e.g., a view, JSON, or redirect). 
View Result: 
  • The view engine processes the view result returned by the action method. 
  • It locates the corresponding view file (.cshtml) based on conventions. 
  • The view is rendered, and dynamic data is injected into the HTML. 
View Engine and View: 
  • The view engine compiles the view into HTML. 
  • The final HTML content is sent as the response to the client. 
  • The client’s browser renders the page. 
Application_End (Optional): 
  • If the application pool recycles or certain thresholds are exceeded, the Application_End event in Global.asax.cs is fired. 
  • This event allows you to perform cleanup tasks before the application shuts down.
Share:

Frequency of character in string

Using Dictionary:
string input = "Mahindra Thar Armada";
Dictionary charFrequency = new Dictionary();

foreach (char c in input)
{
    if (charFrequency.ContainsKey(c))
    {
        charFrequency[c]++;
    }
    else
    {
        charFrequency[c] = 1;
    }
}

// Print the character frequencies
foreach (var pair in charFrequency)
{
    Console.WriteLine($"{pair.Key} - {pair.Value}");
}

Using Linq Query:
var charFrequency = input
    .GroupBy(c => c)
    .ToDictionary(g => g.Key, g => g.Count());

// Print the character frequencies
foreach (var pair in charFrequency)
{
    Console.WriteLine($"{pair.Key} - {pair.Value}");
}
Using JavaScript
const frequencyOfChar = (arr) => {
    return arr.reduce((total, letter) => {
        total[letter] ? total[letter]++ : (total[letter] = 1);
        return total;
    }, {});
};


let input="Mahindra Thar Armada";
let arr=[...input];
const result = frequencyOfChar(arr);
console.log(result); // {"M": 1,"a": 5,"h": 2, "i": 1, "n": 1, "d": 2, "r": 3, " ": 2, "T": 1, "A": 1, "m": 1 }
Share: