Questions : How do you pass parameters to a method by reference in C#? What is the difference between "ref" and "out" keywords in C#? Explain the concept of boxing and unboxing in C#. What are generics in C#? How do you define a generic class in C#? C# : Interview questions (61-65) Answers : 1. Passing Parameters by Reference in C#: To pass parameters by reference in C#, you use the ref or out keywords. This allows the method to modify the argument's value, and the changes will be reflected outside the method. public void ModifyValue ( ref int number ) { number = number * 2 ; } // Usage int myNumber = 5 ; ModifyValue( ref myNumber); Console.WriteLine(myNumber); // Output: 10 In this example, the ModifyValue method doubles the value of myNumber . The ref keyword ensures that the change is reflected outside the method. 2. Difference Between "ref" and "out" Keywords in C#: ref : Requires that the variable be initialized befo...
Read - Revise - Recollect