Skip to main content

Posts

Showing posts with the label interview questions

C# : Interview questions (96-100)

  Questions: How do you invoke a method using reflection in C#? What is the purpose of the "dynamic" keyword in C#? How do you work with dynamic types in C#? What is the "var" keyword in C#? What is the purpose of the "out" keyword in C#? C# : Interview questions (91-95) Answers: 1. Invoking a Method Using Reflection in C#: To invoke a method using reflection in C#, you typically follow these steps: Get the Type of the class that contains the method using typeof or GetType() . Use GetMethod to obtain a MethodInfo object representing the method. Optionally, use GetParameters to get information about the method's parameters. Invoke the method using Invoke on an instance (for instance methods) or null (for static methods), passing in any required parameters. using System; using System.Reflection; public class Calculator { public int Add ( int a, int b ) { return a + b; } } public class Program { public stati...

C# : Interview questions (91-95)

  Questions: How do you implement a switch statement with pattern matching in C#? What are attributes in C#? How do you define custom attributes in C#? Explain the use of reflection in C#. What is the difference between "typeof" and "GetType()" in C#? C# : Interview questions (86-90) Answers: 1. Implementing a Switch Statement with Pattern Matching in C#: Pattern matching in switch statements allows you to match against the type and properties of an object. This can make your code more readable and expressive. public abstract class Shape { } public class Circle : Shape { public double Radius { get ; set ; } } public class Rectangle : Shape { public double Width { get ; set ; } public double Height { get ; set ; } } public static string GetShapeInfo ( Shape shape ) { return shape switch { Circle c => $"Circle with radius {c.Radius} " , Rectangle r => $"Rectangle with width {r.Width} ...

C# : Interview questions (86-90)

  Questions: Explain the concept of null-conditional operators. What are nullable types in C#? How do you define a nullable type in C#? What is the purpose of the "?? operator" in C#? Explain the use of the "switch" statement in C#. C# : Interview questions (81-85) Answers: 1. Concept of Null-Conditional Operators: The null-conditional operators ( ?. and ?[] ) in C# allow you to perform member and element access operations only when the operand is not null . They help prevent NullReferenceException and make the code more concise and readable. ?. Operator: Used for accessing members (properties, methods) safely. Person person = null ; string name = person?.Name; // name will be null if person is null Console.WriteLine(name); // Output: (null) Person person2 = new Person { Name = "John" }; string name2 = person2?.Name; // name2 will be "John" if person2 is not null Console.WriteLine(name2); // Output: John ?[] Operator: Used for safe...

C# : Interview questions (81-85)

  Questions: What is the purpose of the "as" keyword in C#? Explain the difference between "as" and "is" keywords in C#. What is the purpose of the "nameof" operator in C#? How do you implement conditional access in C#? What is pattern matching in C#? C# : Interview questions (76-80) Answers: 1. Purpose of the "as" Keyword in C#: The as keyword in C# is used for safe type casting. It attempts to cast an object to a specified type, and if the cast fails, it returns null instead of throwing an exception. This is useful for avoiding exceptions when you are unsure if the conversion will succeed. object obj = "Hello, world!" ; string str = obj as string ; if (str != null ) { Console.WriteLine(str); // Output: Hello, world! } In this example, obj is safely cast to string using the as keyword. If obj were not a string, str would be null . 2. Difference Between "as" and "is" Keywords in C#: is : C...

C# : Interview questions (76-80)

  Questions: What is a tuple in C#? How do you create a tuple in C#? Explain the concept of indexers in C#. What is the purpose of the "yield" keyword in C#? How do you implement iterator methods in C#? C# : Interview questions (71-75) Answers: 1. What is a Tuple in C#? A tuple in C# is a data structure that allows you to store a fixed-size collection of heterogeneously-typed elements. It provides a way to group multiple values together without having to define a specific class or struct. 2. How to Create a Tuple in C#: Tuples can be created using the Tuple class or the newer ValueTuple struct introduced in C# 7.0. The ValueTuple is more efficient and easier to use. // Using the Tuple class (pre C# 7.0) Tuple< int , string , bool > person1 = new Tuple< int , string , bool >( 1 , "John" , true ); // Using the ValueTuple (C# 7.0 and later) var person2 = (Id: 1 , Name: "John" , IsActive: true ); // Accessing tuple elements Console.WriteL...

C# : Interview questions (71-75)

Questions: What is a generic method? Explain the use of constraints in generics. What is covariance and contravariance in generics? What are anonymous types in C#? How do you implement anonymous methods in C#? C# : Interview questions (66-70) Answers: 1. What is a Generic Method? A generic method in C# is a method that is defined with a type parameter. This allows the method to operate on different data types without needing to be rewritten for each type. Generic methods provide type safety, reduce code duplication, and improve performance. public class Utilities { public void Swap < T >( ref T a, ref T b ) { T temp = a; a = b; b = temp; } } // Usage int x = 1 , y = 2 ; Utilities utilities = new Utilities(); utilities.Swap( ref x, ref y); Console.WriteLine( $"x: {x} , y: {y} " ); // Output: x: 2, y: 1 In this example, the Swap method is a generic method that can swap the values of two variables of any type. 2. Use of C...

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...