Skip to content
Keshav Singh edited this page May 28, 2023 · 15 revisions

C_Sharp

Filters

Filters allow us to run custom code before or after executing the action method. They provide ways to do common repetitive tasks on our action method. The filters are invoked on certain stages in the request processing pipeline.

  • Authorization FIlter
  • Resource Filter
  • Action Filter
  • Result Filter
  • Exception Filter

A filter can be added to the pipeline at one of three scopes

  • by action method,
  • by controller class or
  • globally (which be applied to all the controller and actions).

We need to register filters in to the MvcOption.Filters collection within ConfigureServices method.

Filter types overview

Implementation of filter overview

We can apply our filter to the controller class or action method using one of the following

  • ServiceFilterAttribute
[ServiceFilter(typeof(ExampleFilterWithDI))]  
public IActionResult Index()  
{  
    return View();  
}
  • TypeFilterAttribute
    • It is very similar to ServiceFilterAttribute and also implemented from IFilterFactory interface.
    • The "TypeFilterAttribute" can be optionally accept constructor arguments for the type.
[TypeFilter(typeof(ExampleFilterAttribute), Arguments = new object[] {"Argument if any" })]  
public IActionResult About()  
{  
    return View();  
}
  • IFilterFactory implemented on attribute

References:


Ref and Out

  • ref tells the compiler that the object is initialized before entering the function, while out tells the compiler that the object will be initialized inside the function.

  • So while ref is two-ways, out is out-only.

Ref Out
The parameter or argument must be initialized first before it is passed to ref. It is not compulsory to initialize a parameter or argument before it is passed to an out.
It is not required to assign or initialize the value of a parameter (which is passed by ref) before returning to the calling method. A called method is required to assign or initialize a value of a parameter (which is passed to an out) before returning to the calling method.
Passing a parameter value by Ref is useful when the called method is also needed to modify the pass parameter. Declaring a parameter to an out method is useful when multiple values need to be returned from a function or method.
It is not compulsory to initialize a parameter value before using it in a calling method. A parameter value must be initialized within the calling method before its use.
When we use REF, data can be passed bi-directionally. When we use OUT data is passed only in a unidirectional way (from the called method to the caller method).
Both ref and out are treated differently at run time and they are treated the same at compile time.

References:


Boxing and Unboxing

  • Boxing

    • The conversion of value type to reference type is known as boxing. Boxing
  • Unboxing

    • The conversion of reference type to value is known as unboxing. UnBoxing

References:


DI

  • It is a design pattern that allows objects to depend on other objects, called dependencies, without creating them directly.
  • It is a software design pattern which enables the development of loosely coupled code. Through DI, you can decrease tight coupling between software components. It is also known as Inversion-of-Control.

There are three types of DIs

    Constructor Injection
    Setter Injection
    Method Injection

There are 3 types of lifetimes supported by ASP.NET Core for the dependency injection

  • Transient Service
    • New instance transient service is created whenever the service is requested.
  • Scoped Service
    • Created once per scope; i.e., web request.or any unit of work.
  • Singleton Service
    • Singleton service is only created when it is called for the first time. In the next subsequent requests, the same instance is provided.

References

What are the features of C#?

  1. Object-Oriented Programming

    C# is an object-oriented programming (OOP) language. It supports features such as classes, objects, encapsulation, inheritance, and polymorphism.
    
  2. Type Safety

    It is a type-safe language, which means that it enforces type checking at compile time to ensure that variables are used only in the ways intended by the programmer. Example:-
    
    int x = 10;   // declaring an integer variable and initializing it with value 10
    string str = "Hello, World!";   // declaring a string variable and initializing it with a string value
    
    // We cannot assign a string value to an integer variable:
    x = str;   // This will result in a compilation error
    
    // Similarly, we cannot call a method on a variable that doesn't support it:
    int y = 5;
    y.ToUpper();   // This will result in a compilation error since ToUpper() method 
  3. Garbage Collection

    C# includes automatic garbage collection, which automatically frees up memory that is no longer being used by the program.
    
  4. Cross-Platform Support

    C# can be used to develop applications for a wide range of platforms, including Windows, macOS, Linux, and mobile devices.
    
  5. Language Interoperability

    C# can interoperate with other programming languages, including C, C++, and Visual Basic.
    
  6. LINQ

    Language Integrated Query (LINQ) is a powerful feature in C# that allows developers to query and manipulate data from different data sources using a uniform syntax.
    
  7. Asynchronous Programming

    C# includes support for asynchronous programming, which allows developers to write code that can execute concurrently without blocking the main thread.
    
  8. Exception Handling

    C# includes robust exception handling capabilities that allow developers to handle and recover from runtime errors in a structured manner.
    
  9. Delegates and Events

    C# supports the use of delegates and events, which are used for implementing callback functions and event-driven programming.
    
  10. Security Features

    C# includes security features such as code access security, which helps protect against malicious code execution, and cryptography, which allows for secure communication and data storage.
    

What is Asynchronous and synchronous with example

  • Synchronous refers to an operation that blocks the execution of the program until the operation completes

  • Asynchronous, on the other hand, refers to an operation that does not block the execution of the program but instead executes in the background, allowing the program to continue executing other operations.

Here's an example of synchronous code

public static void Main()
{
    Console.WriteLine("Starting synchronous operation...");
    string result = GetDataFromWeb(); // This method blocks the execution until it gets the data
    Console.WriteLine("Synchronous operation completed with result: " + result);
}

public static string GetDataFromWeb()
{
    // This method makes a web request to get some data
    WebClient client = new WebClient();
    string result = client.DownloadString("http://example.com");
    return result;
}

In the above code, the Main method makes a call to the GetDataFromWeb method, which makes a web request to retrieve some data. Since this operation is synchronous, the execution of the program is blocked until the web request completes and returns the data. Only then does the program continue to execute the next line of code.

Here's an example of asynchronous code

public static async Task Main()
{
    Console.WriteLine("Starting asynchronous operation...");
    Task<string> resultTask = GetDataFromWebAsync(); // This method executes in the background
    Console.WriteLine("Asynchronous operation started, program continues executing other operations...");

    // Do some other work here while the GetDataFromWebAsync() method is executing in the background

    string result = await resultTask; // This line blocks until the GetDataFromWebAsync() method completes and returns the data
    Console.WriteLine("Asynchronous operation completed with result: " + result);
}

public static async Task<string> GetDataFromWebAsync()
{
    // This method makes an asynchronous web request to get some data
    HttpClient client = new HttpClient();
    string result = await client.GetStringAsync("http://example.com");
    return result;
}

In the above code, the Main method makes a call to the GetDataFromWebAsync method, which makes an asynchronous web request to retrieve some data. Since this operation is asynchronous, the execution of the program is not blocked and the program continues executing other operations while the web request is being made in the background. The program then waits for the result of the asynchronous operation using the await keyword, which does not block the execution of the program but instead waits for the result to become available before continuing to execute the next line of code.


Delegates and Events

  • A delegate is a type that represents a reference to a method with a particular parameter list and return type
public delegate int BinaryOperation(int x, int y);

public class Calculator
{
    public int Add(int x, int y)
    {
        return x + y;
    }

    public int Subtract(int x, int y)
    {
        return x - y;
    }
}

public static void Main()
{
    Calculator calculator = new Calculator();

    BinaryOperation operation = new BinaryOperation(calculator.Add);

    int result = operation(5, 3); // result = 8

    operation = new BinaryOperation(calculator.Subtract);

    result = operation(5, 3); // result = 2
}
  • An event is a construct built on top of delegates that allows objects to be notified when an event occurs.
public class Button
{
    public event EventHandler Clicked;

    public void Click()
    {
        if (Clicked != null)
        {
            Clicked(this, EventArgs.Empty);
        }
    }
}

public static void Main()
{
    Button button = new Button();

    button.Clicked += OnButtonClicked;

    button.Click();
}

public static void OnButtonClicked(object sender, EventArgs e)
{
    Console.WriteLine("Button clicked!");
}

Method signature

  • The signature of a method in C# consists of its name and the types of its parameters. The return type of the method is not considered part of its signature.

Comments

In c#, there are three types of comments available, those are

  • Single-line Comments
// Single Line Comment 
  • Multi-line Comments
/* Multi Line Comment */
  • XML Comments
///<summary>
/// This class does something.
///</summary>

Indexer

  • It allows instances of a class or structure to be indexed same like an array.
public class UserIndexer
{
    public Dictionary<string, string> Users = new Dictionary<string, string>();

    public string this[string index]
    {
        get { return Users[index]; }
        set { Users[index] = value; }
    }
}

UserIndexer user = new();

user["Name"] = "Keshav";
user["password"] = "1234";


Console.WriteLine($"Name: {user["Name"]}");
Console.WriteLine($"Password: {user["password"]}");

/*Output:
Name: Keshav
Password: 1234
*/

Overload Indexer

public class UserIndexer
{
    public Dictionary<string, string> Users = new();
    public Dictionary<char, int> Chars = new();

    public string this[string index]
    {
        get { return Users[index]; }
        set { Users[index] = value; }
    }

    public int this[char index]
    {
        get { return Chars[index]; }
        set { Chars[index] = value; }
    }
}

UserIndexer user = new();

user["Name"] = "Keshav";
user["password"] = "1234";
user['A'] = 65;
user['C'] = 67;


Console.WriteLine($"Name: {user["Name"]}");
Console.WriteLine($"Password: {user["password"]}");
Console.WriteLine($"A ASCII value {user['A']}");
Console.WriteLine($"C ASCII value {user['C']}");

/*Output:
Name: Keshav
Password: 1234
A ASCII value 65
C ASCII value 67
*/

C# Indexer Overview

Following are the important points which we need to remember about indexers in c#.

  • In c#, the indexer is a special type of property, allowing instances of a class or structure to be indexed same as an array.
  • In c#, the indexer is same as the property, but the only difference is, the indexer will define with this keyword along with the square bracket and parameters.
  • Indexers can be overloaded by having different signatures.
  • Indexers cannot be a static member as it’s an instance member of the class.
  • Passing indexer value as a ref or out parameter is not supported.

In C#, indexers are instance members that allow objects of a class to be indexed in a similar way as arrays. They cannot be declared as static because they are used to access or modify the data of individual instances of a class, not shared data across all instances.

References