Skip to main content

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#?
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 is a variable declared directly in a class or struct.
    • Fields can be public, private, protected, or internal.
    • Fields are accessed directly.
public class Person
{
    public string name; // Field
}
  • Property:
    • A property provides a controlled way to access the private fields of a class.
    • Properties can include logic in their accessors.
    • Properties can be read-only, write-only, or read-write.
    • Properties use get and set accessors.
public class Person
{
    private string name;

    public string Name
    {
        get { return name; }
        set { name = value; }
    }
}

3. Purpose of the "this" Keyword in C#:

The this keyword in C# refers to the current instance of the class. It is used to access members (fields, properties, methods) of the current instance, especially when there is a naming conflict between method parameters and class members.

public class Person
{
    private string name;

    public void SetName(string name)
    {
        this.name = name; // "this" refers to the current instance's "name" field
    }
}

In this example, the this keyword distinguishes the name field of the current instance from the name parameter of the SetName method.

4. Creating an Indexer in C#:

An indexer in C# allows an object to be indexed like an array. Indexers are defined using the this keyword followed by square brackets.

public class StringCollection
{
    private string[] collection = new string[10];

    public string this[int index]
    {
        get { return collection[index]; }
        set { collection[index] = value; }
    }
}

In this example, the StringCollection class has an indexer that allows access to the collection array using an index.

5. Purpose of the "base" Keyword in C#:

The base keyword in C# is used to access members of the base class from within a derived class. It can be used to call base class constructors, methods, or access fields and properties.

public class BaseClass
{
    public void Display()
    {
        Console.WriteLine("Base class method");
    }
}

public class DerivedClass : BaseClass
{
    public void Show()
    {
        base.Display(); // Call method from base class
        Console.WriteLine("Derived class method");
    }
}

In this example, the base keyword is used in the Show method of the DerivedClass to call the Display method from the BaseClass.

These explanations and examples should help you understand the implementation and use of these concepts in C#.

C# : Interview questions (61-65)

Comments

Popular posts from this blog

C# : How can we access private method outside class

Introduction In object-oriented programming, encapsulation is a fundamental principle that restricts direct access to the internal implementation details of a class. Private methods, being part of this internal implementation, are designed to be accessible only within the confines of the class they belong to. However, there might be scenarios where you need to access a private method from outside the class. In this blog post, we'll explore several techniques to achieve this in C#. 1. Reflection: A Powerful Yet Delicate Approach Reflection is a mechanism in C# that allows inspecting and interacting with metadata about types, fields, properties, and methods. While it provides a way to access private methods, it should be used cautiously due to its potential impact on maintainability and performance. using System ; using System . Reflection ; public class MyClass { private void PrivateMethod ( ) { Console . WriteLine ( "This is a private method."

C# : Understanding Types of Classes

In C#, classes serve as the building blocks of object-oriented programming, providing a blueprint for creating objects. Understanding the types of classes and their applications is crucial for designing robust and maintainable software. In this blog, we’ll delve into various types of classes in C#, accompanied by real-world scenarios and code snippets for a practical understanding. 1. Regular (Instance) Classes Definition: Regular classes are the most common type and are used to create instances or objects. They can contain fields, properties, methods, and other members. Example Scenario: A Person class representing individual persons with properties like Name and Age. public class Person { public string Name { get ; set ; } public int Age { get ; set ; } } 2. Static Classes Definition: A static class cannot be instantiated and can only contain static members (methods, properties, fields). It’s often used for utility functions. Example Scenario: A MathUtility cla

20+ LINQ Concepts with .Net Code

LINQ   (Language Integrated Query) is one of the most powerful features in .NET, providing a unified syntax to query collections, databases, XML, and other data sources. Below are 20+ important LINQ concepts, their explanations, and code snippets to help you understand their usage. 1.  Where  (Filtering) The  Where()  method is used to filter a collection based on a given condition. var numbers = new List < int > { 1 , 2 , 3 , 4 , 5 , 6 } ; var evenNumbers = numbers . Where ( n => n % 2 == 0 ) . ToList ( ) ; // Output: [2, 4, 6] C# Copy 2.  Select  (Projection) The  Select()  method projects each element of a sequence into a new form, allowing transformation of data. var employees = new List < Employee > { /* ... */ } ; var employeeNames = employees . Select ( e => e . Name ) . ToList ( ) ; // Output: List of employee names C# Copy 3.  OrderBy  (Sorting in Ascending Order) The  OrderBy()  method sorts the elements of a sequence in ascendi