Skip to main content

Posts

Showing posts from March, 2018

Difference Between Dynamic and Object Keyword in C#

The dynamic keyword and objects don't perform compile-time type checks and identify the type of objects at runtime only and both can store any type of the variable. Objects were introduced in 1.0 C#. Later, why was Dynamic introduced in 4.0 C# when Objects already exist. Difference 1 : Object: the Compiler has little information about the type. It's not compiler safe. You need to do an explicit type casting every time you want to get the value back and forth. object  a = "Nikhil Sangani"; string a1 = a.ToString(); Dynamic: In a dynamic variable, the compiler doesn't have any information about the type of variable. dynamic  a = "Nikhil Sangani"; string a1 = a; Difference 2 : Object: When using an object, you need to cast the object variable to the original type to use it and do the desired operations. In the first difference, the following example shows an error. object  a = "Nikhil Sangani"; string a1 = a; Now you need t

Command Query Separation (CQS) in C#

CQS is the principle of development where we need to separate command from the query. It's a basic rule that a developer must follow. Segregation of command with the query is very important. Command: Which change the state of a system. In event-driven programming, the function that affects the system's state is a command. The method whose return type is void is supposed to be command type. Example: A button click event in web form is a command as it changes the state of the system. protected void register_Click(object sender, EventArgs e) {     // your code to registeration } Query :  The query can be defined as a method that has a return type. A query must return something. This is like you are asking something from the system. Example:  public bool IsValidUserName(string UserName) {     bool validUser = false;     //code to check user from db     //if user exist set validUser=true else false     return validUser; } This method is used to check the val

Different Types of Software Design Principles

1. SOLID : It is a combination of five basic designing principles. A. Single Responsibility Principle (SRP) This principle states that there should never be more than one reason for a class to change. This means that you should design your classes in such a way that each class should have a single purpose. Example - An Account class is responsible for managing Current and Saving Account but a CurrentAccount and a Saving Account classes would be responsible for managing current and savings accounts respectively. Hence both are responsible for a single purpose only. Hence we are moving towards specialization. B. Open/Closed Principle (OCP) This principle states that software entities (classes, modules, functions, etc.) should be open for extension but closed for modification. The "closed" part of the rule states that once a module has been developed and tested, the code should only be changed to correct bugs. The "open" part says that you should be able to e

Finding Foreign Key In SQL Server

Here I Will explain how many ways, we can find a foreign key in SQL Server. Using this query, we can find all the foreign keys in the current database. select * from sysobjects where type='f' Using this query, we can find all the foreign keys in a selected table. EXEC sp_fkeys 'Catalog' Using this query, we can find all the foreign keys in a current database with more information. SELECT RC.CONSTRAINT_NAME FK_Name , KF.TABLE_SCHEMA FK_Schema , KF.TABLE_NAME FK_Table , KF.COLUMN_NAME FK_Column , RC.UNIQUE_CONSTRAINT_NAME PK_Name , KP.TABLE_SCHEMA PK_Schema , KP.TABLE_NAME PK_Table , KP.COLUMN_NAME PK_Column , RC.MATCH_OPTION MatchOption , RC.UPDATE_RULE UpdateRule , RC.DELETE_RULE DeleteRule FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS RC JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE KF ON RC.CONSTRAINT_NAME = KF.CONSTRAINT_NAME JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE KP ON RC.UNIQUE_CONSTRAINT_NAME = KP.CONSTRAINT_NAME

Remove Duplicate Records From A Table In SQL Server

Here I Will explain how to remove duplicate records from a table in SQL Server.  First of all I have created on table with following columns. CREATE TABLE #Employee   ( EmployeId int IDENTITY(1,1) NOT NULL, Name varchar(55) NULL, Salary decimal(10, 2) NULL, Designation varchar(20) NULL ) After that add a same record multiple time. INSERT INTO #Employee values('Nikhil' ,44000 ,'Web Developer') INSERT INTO #Employee values('Nikhil' ,44000 ,'Web Developer') INSERT INTO #Employee values('Nikhil' ,44000 ,'Web Developer') INSERT INTO #Employee values('Nikhil' ,44000 ,'Web Developer') INSERT INTO #Employee values('Nikhil' ,44000 ,'Web Developer') INSERT INTO #Employee values('Nikhil' ,44000 ,'Web Developer') After that execute a query to remove duplicate records. WITH TempEmp (Name,duplicateRecCount) AS ( SELECT Name,ROW_NUMBER() OVER(PARTITION by Name,

Difference Between Primary key, Unique key And Foreign Key.

The difference between Primary key, Unique key and Foreign Key is the most common interview question for .NET as well as SQL developers.  The PRIMARY Key and UNIQUE Key constraints, both are similar and enforce uniqueness of the column on which they are defined. Primary Key A primary key cannot have a NULL value. Each table can have only one primary key. By default, Primary key is clustered index, and the data in the database table is physically organized in the sequence of the clustered index. A primary key can be related to other tables as a Foreign Key. We can generate ID automatically with the help of Auto Increment field. Primary key supports Auto Increment value. We can define a Primary key constraint on temporary table and table variable.  We can't delete a primary key value from the parent table which is used as a foreign key in the child table. To delete we first need to delete that primary key value from the child table. Unique Key Unique Const