Friday 26 February 2021

ref vs Out parameters

 

class Program

    {

        static void Main(string[] args)

        {

            //The ref is a keyword in C# which is used for the passing the arguments by a reference.

            string str1 = "Geek";//It is necessary the parameters should initialize before it pass to ref.

            SetValue1(ref str1);

            Console.WriteLine(str1);//When ref keyword is used the data may pass in bi-directional.

 

            //SetValue1(ref string refObj);//Error you cannot declare while passing ref parameter

 

            //The out is a keyword in C# which is used for the passing the arguments to methods as a reference type.

            string str2 = "Geek";

            SetValue2(out str2);

            //SetValue2(out string str2);//No Error//Comment above two lines to debug the code

            Console.WriteLine(str2);//When out keyword is used the data only passed in unidirectional.

           

            string str3 = "Geek3";

            string str4 = "Geek4";

            SetValue3(ref str3, ref str4);//reference can be used for passing multiple parameters

            Console.WriteLine(str3 + " " + str4);//When ref keyword is used the data may pass in bi-directional.

 

            string str5 = "Geek5";

            string str6 = "Geek6";

            SetValue4(out str5, out str6);//When out keyword is used the data only passed in unidirectional.

            Console.WriteLine(str5 + " " + str6);

        }

 

        static void SetValue1(ref string str1)

        {           

            if (str1 == "Geek")//It is not necessary to initialize the value of a parameter before returning to the calling method.

            {

                Console.WriteLine("Hello!!" + str1);

            }

            str1 = "GeeksforGeeks";

        }

 

        static void SetValue2(out string str1)

        {

            str1 = "";//It is necessary to initialize the value of a parameter before returning to the calling method.

            if (str1 == "Geek")//if you comment above line it will throw error

            {

                Console.WriteLine("Hello!!Geek");

            }

            str1 = "GeeksforGeeks";

        }

 

        static void SetValue3(ref string str3, ref string str4)

        {           

            if (str3 == "Geek3")

            {

                Console.WriteLine("Hello!!" + str3);

            }

            if (str4 == "Geek4")

            {

                Console.WriteLine("Hello!!" + str4);

            }

            str3 = "GeeksforGeeks3";

            str4 = "GeeksforGeeks4";

        }

        static void SetValue4(out string str5, out string str6)

        {

            str5 = "";//It is necessary to initialize the value of a parameter before returning to the calling method.

            str6 = "";

 

            str5 = "GeeksforGeeks5";

            str6 = "GeeksforGeeks6";

        }

 

    }

Sunday 21 February 2021

Saturday 20 February 2021

C#: Define what is Let clause?

 In a query expression, it is sometimes useful to store the result of a sub-expression in order to use it in subsequent clauses. You can do this with the let keyword, which creates a new range variable and initializes it with the result of the expression you supply.

 

 

var names = new string[] { "Dog", "Cat", "Giraffe", "Monkey", "Tortoise" };
var result =
    from animalName in names
    let nameLength = animalName.Length
    where nameLength > 3
    orderby nameLength
    select animalName; 

 Reference: Kill Your Tech Interview

SQL Server: What is the purpose of ROWLOCK on Delete and when should I use it?

 

Rowlock is a query hint that should be used with caution (as is all query hints).

Omitting it will likely still result in the exact same behaviour and providing it will not guarantee that it will only use a rowlock, it is only a hint afterall. If you do not have a very in depth knowledge of lock contention chances are that the optimizer will pick the best possible locking strategy, and these things are usually best left to the database engine to decide.

ROWLOCK means that SQL will lock only the affected row, and not the entire table or the page in the table where the data is stored when performing the delete. This will only affect other people reading from the table at the same time as your delete is running.

If a table lock is used it will cause all queries to the table to wait until your delete has completed, with a row lock only selects reading the specific rows will be made to wait.

Deleting top N where N is a number of rows will most likely lock the table in any case.

 

Reference: What is the purpose of ROWLOCK on Delete and when should I use it?

Thursday 18 February 2021

Angular: ng.ps1 cannot be loaded because running scripts is disabled on this system on angular.

Solution: Run the following command from the same terminal or command prompt and re-run the ng command to check if it works on your machine:

Sol 1:  Set-ExecutionPolicy RemoteSigned -Scope CurrentUser

Sol 2: make sure that you are running the command in the application root folder..

 


 

 

Ref: ng : File \AppData\Roaming\npm\ng.ps1 cannot be loaded. The file npm\ng.ps1 is not digitally signed Angular Error when running commands

Ref: Angular CLI Error: The serve command requires to be run in an Angular project, but a project definition could not be found