Skip to main content

Posts

Showing posts from June, 2024

C# : Interview questions (66-70)

  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

C# : Interview questions (61-65)

Questions : How do you implement method overloading in C#? Explain the concept of extension methods. What is a partial class in C#? How do you implement operator overloading in C#? What is the purpose of the "params" keyword in C#? C# : Interview questions (51-55) Answers : 1. Implementing Method Overloading in C#: Method overloading in C# allows you to define multiple methods with the same name but different parameters (number, type, or both). The correct method to be invoked is determined by the arguments passed during the method call public class MathOperations { // Method to add two integers public int Add ( int a, int b ) { return a + b; } // Overloaded method to add three integers public int Add ( int a, int b, int c ) { return a + b + c; } // Overloaded method to add two doubles public double Add ( double a, double b ) { return a + b; } } In this example, the Add method

C# : Interview questions (56-60)

  Questions : How do you implement a property with both get and set accessors? Explain the difference between a property and a field. What is the purpose of the "this" keyword in C#? How do you create an indexer in C#? What is the purpose of the "base" keyword in C#? C# : Interview questions (51-55) Answers : 1. Implementing a Property with Both Get and Set Accessors: A property in C# can have both get and set accessors to encapsulate the access to a private field. The get accessor is used to return the property value, and the set accessor is used to assign a new value to the property. public class Person { private string name; public string Name { get { return name; } set { name = value ; } } } In this example, the Name property encapsulates the private field name . The get accessor returns the value of name , and the set accessor assigns a new value to name . 2. Difference Between a Property and a Field: Field: A field