Wednesday 26 May 2021
Abstract class, method & Interface in Factory pattern method.
Sunday 23 May 2021
C# Concepts to learn
1. Class
Everything in C# is associated with classes and objects, along with its attributes and methods.
2.Encapsulation
Encapsulation is implemented by using access specifiers. An access specifier defines the scope and visibility of a class member.
Public-Any public member can be accessed from outside the class.
Private- Access only within a class or scope
Protected- allows a child class to access the member variables and member functions of its base class.
Internal- allows a class to expose its member variables and member functions to other functions and objects in the current assembly.
Protected internal -allows a class to hide its member variables and member functions from other class objects and functions, except a child class within the same application.
3.Polymorphism
polymorphism means having many forms. In object-oriented programming paradigm, polymorphism is often expressed as 'one interface, multiple functions'.
Polymorphism can be static or dynamic. In static polymorphism, the response to a function is determined at the compile time. In dynamic polymorphism, it is decided at run-time.
static polymorphism
- Function overloading ex: void print(int x){}; void print (string x){};
- Operator overloading
Dynamic Polymorphism
C# allows you to create abstract classes that are used to provide partial class implementation of an interface. Implementation is completed when a derived class inherits from it. Abstract classes contain abstract methods, which are implemented by the derived class. The derived classes have more specialized functionality.
Dynamic polymorphism is implemented by abstract classes and virtual functions.
Here are the rules about abstract classes −
You cannot create an instance of an abstract class
You cannot declare an abstract method outside an abstract class
When a class is declared sealed, it cannot be inherited, abstract classes cannot be declared sealed.
4.Inheritance
it's an concept of base class and derived class. derived class can access base class member and member functions.
-Single inheritance( class A{}; sealed class B:A{};
-Multilevel inheritance ( class A{}; class B:A{}; class C:B)
-Multiple not possible but can achieve through interface.
5.Interface
it contains function declaration only with default public access specifies, implemented/derived class should provide definition declared functions in interface.
Public interface ICalculator
{
int sum(int x,int y);
}
public class Calc : ICalculator
{
public int sum(int x,int y){ return x+y;}
}
6.Abstraction
Data abstraction is the process of hiding certain details and showing only essential information to the user.
Abstraction can be achieved with either abstract classes or interfaces (which you will learn more about in the next chapter).
The abstract
keyword is used for classes and methods:
- Abstract class: is a restricted class that cannot be used to create objects (to access it, it must be inherited from another class).
- Abstract method: can only be used in an abstract class, and it does not have a body. The body is provided by the derived class (inherited from).
An abstract class can have both abstract and regular methods:
// Abstract class
abstract class Animal
{
// Abstract method (does not have a body)
public abstract void animalSound();
// Regular method
public void sleep()
{
Console.WriteLine("Zzz");
}
}
// Derived class (inherit from Animal)
class Pig : Animal
{
public override void animalSound()
{
// The body of animalSound() is provided here
Console.WriteLine("The pig says: wee wee");
}
}
class Program
{
static void Main(string[] args)
{
Pig myPig = new Pig(); // Create a Pig object
myPig.animalSound(); // Call the abstract method
myPig.sleep(); // Call the regular method
}
}
7.Dependancy Injection:
8.Scafolding - Auto generated code for Controller CRUD operation using Entity Framework.
9.Filters
10. Diff bw Authorize and Authentication
Authentication : need to pass login credentials, it's valid then it will return authorize response.
Authorize: after Authentication steps based on response it will validate user access to the particular action.
11.MVC architecture
12. ViewBag and ViewData and TempData
ViewData, ViewBag and TeampData for passing data from controller to view and in next request. ViewData and ViewBag are almost similar and it helps us to transfer the data from controller to view whereas TempData also works during the current and subsequent requests.
ViewData | ViewBag | TempData |
It is Key-Value Dictionary collection | It is a type object | It is Key-Value Dictionary collection |
ViewData is a dictionary object and it is property of ControllerBase class | ViewBag is Dynamic property of ControllerBase class. | TempData is a dictionary object and it is property of controllerBase class. |
ViewData is Faster than ViewBag | ViewBag is slower than ViewData | NA |
ViewData is introduced in MVC 1.0 and available in MVC 1.0 and above | ViewBag is introduced in MVC 3.0 and available in MVC 3.0 and above | TempData is also introduced in MVC1.0 and available in MVC 1.0 and above. |
ViewData also works with .net framework 3.5 and above | ViewBag only works with .net framework 4.0 and above | TempData also works with .net framework 3.5 and above |
Type Conversion code is required while enumerating | In depth, ViewBag is used dynamic, so there is no need to type conversion while enumerating. | Type Conversion code is required while enumerating |
Its value becomes null if redirection has occurred. | Same as ViewData | TempData is used to pass data between two consecutive requests. |
It lies only during the current request. | Same as ViewData | TempData only works during the current and subsequent request |
13.Seesion, Temp Data and Cookies
Session: stored in server side we can access all actions until get expired
Temp Data: stored in server side we can access all actions once read the value it will get expired.
Cookies: Stored in client side Each request & response it will pass. not secured one so not add sensitive info
14.Action Verbs
Action Verbs supported by MVC framework are HttpGet, HttpPost, HttpPut, HttpDelete, HttpOptions & HttpPatch, NonAction
15.Areas in MVC
16.Partial View
17.Partial Class
18.Inner class
19.Linq
- Sum
-Count
-Group by
- Min & Max
-Join
20. Quarriable & Enumerable
21. Render Action methods
22.Routing
23.Startup config
24.Constructors & types
25.Method Overload & Override
When two or more methods in the same class have the same name but different parameters, it's called Overloading. When the method signature (name and parameters) are the same in the superclass and the child class, it's called Overriding.
26.Singleton class and object
Singleton :In this pattern, a class has only one instance in the program that provides a global point of access to it. In other words, a singleton is a class that allows only a single instance of itself to be created and usually gives simple access to that instance.
The advantages of a Singleton Pattern are,
- Singleton pattern can implement interfaces.
- Can be lazy-loaded and has Static Initialization.
- It helps to hide dependencies.
- It provides a single point of access to a particular instance, so it is easy to maintain.
- No Thread Safe Singleton.
- Thread-Safety Singleton.
- Thread-Safety Singleton using Double-Check Locking.
- Thread-safe without a lock.
- Using .NET 4's Lazy<T> type.
27.Sealed class- no more inheritance
28.Dictionary & Hash table
29.Deligate and Callback
C# delegates are similar to pointers to functions, in C or C++. A delegate is a reference type variable that holds the reference to a method. The reference can be changed at runtime.
Delegates are especially used for implementing events and the call-back methods. All delegates are implicitly derived from the System.Delegate class.
30.Global.asax : file allows you to write code that runs in response to application-level events, such as Application_BeginRequest, application_start, application_error, session_start, session_end, etc.
31.Ioc- Inversion Of Control : It is an design principle to support Test Driven Development and Dependency Injection.
https://www.tutorialsteacher.com/ioc/introduction
IoC is a design principle which recommends the inversion of different kinds of controls in object-oriented design to achieve loose coupling between application classes. In this case, control refers to any additional responsibilities a class has, other than its main responsibility, such as control over the flow of an application, or control over the dependent object creation and binding (Remember SRP - Single Responsibility Principle). If you want to do TDD (Test Driven Development), then you must use the IoC principle, without which TDD is not possible. Learn about IoC in detail in the next chapter.
Dependency Inversion Principle
The DIP principle also helps in achieving loose coupling between classes. It is highly recommended to use DIP and IoC together in order to achieve loose coupling.DIP suggests that high-level modules should not depend on low level modules. Both should depend on abstraction.
ref keyword | out keyword |
---|---|
It is necessary the parameters should initialize before it pass to ref. | It is not necessary to initialize parameters before it pass to out. |
It is not necessary to initialize the value of a parameter before returning to the calling method. | It is necessary to initialize the value of a parameter before returning to the calling method. |
The passing of value through ref parameter is useful when the called method also need to change the value of passed parameter. | The declaring of parameter through out parameter is useful when a method return multiple values. |
When ref keyword is used the data may pass in bi-directional. | When out keyword is used the data only passed in unidirectional. |
Sunday 16 May 2021
Access spcifiers in C#
protected: The type or member can be accessed only by code in the same class or struct, or in a class that is derived from that class.internal: The type or member can be accessed by any code in the same assembly, but not from another assembly. The "protected internal" access modifier is a union of both the "protected" and "internal" modifiers. From MSDN, Access Modifiers (C# Programming Guide): The type or member can be accessed by any code in the assembly in which it is declared, OR from within a derived class in another assembly. Access from another assembly must take place within a class declaration that derives from the class in which the protected internal element is declared, and it must take place through an instance of the derived class type.Note that: protected internal means "protected OR internal " (any class in the same assembly, or any derived class - even if it is in a different assembly).private The type or member can be accessed only by code in the same class or struct.public The type or member can be accessed by any other code in the same assembly or another assembly that references it. |
Monday 3 May 2021
Get Table & Schema details using SQL Query
This first query will return all of the tables in the database you are querying.
SELECT
*
FROM
INFORMATION_SCHEMA.TABLES
where table_name like '%tablename%'
The second query will return a list of all the columns and tables in the database you are querying.
SELECT
TABLE_NAME,
COLUMN_NAME
FROM
INFORMATION_SCHEMA.COLUMNS
Or, you can also query for just the COLUMNS
from a specific table and return the column names from the specific table ‘Album’ in our database.
SELECT
COLUMN_NAME
FROM
INFORMATION_SCHEMA.COLUMNS
WHERE
TABLE_NAME = 'Album'