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

Implementing and Integrating RabbitMQ in .NET Core Application: Shopping Cart and Order API

RabbitMQ is a robust message broker that enables communication between services in a decoupled, reliable manner. In this guide, we’ll implement RabbitMQ in a .NET Core application to connect two microservices: Shopping Cart API (Producer) and Order API (Consumer). 1. Prerequisites Install RabbitMQ locally or on a server. Default Management UI: http://localhost:15672 Default Credentials: guest/guest Install the RabbitMQ.Client package for .NET: dotnet add package RabbitMQ.Client 2. Architecture Overview Shopping Cart API (Producer): Sends a message when a user places an order. RabbitMQ : Acts as the broker to hold the message. Order API (Consumer): Receives the message and processes the order. 3. RabbitMQ Producer: Shopping Cart API Step 1: Install RabbitMQ.Client Ensure the RabbitMQ client library is installed: dotnet add package RabbitMQ.Client Step 2: Create the Producer Service Add a RabbitMQProducer class to send messages. RabbitMQProducer.cs : using RabbitMQ.Client; usin...

Clean Architecture: What It Is and How It Differs from Microservices

In the tech world, buzzwords like   Clean Architecture   and   Microservices   often dominate discussions about building scalable, maintainable applications. But what exactly is Clean Architecture? How does it compare to Microservices? And most importantly, is it more efficient? Let’s break it all down, from understanding the core principles of Clean Architecture to comparing it with Microservices. By the end of this blog, you’ll know when to use each and why Clean Architecture might just be the silent hero your projects need. What is Clean Architecture? Clean Architecture  is a design paradigm introduced by Robert C. Martin (Uncle Bob) in his book  Clean Architecture: A Craftsman’s Guide to Software Structure and Design . It’s an evolution of layered architecture, focusing on organizing code in a way that makes it  flexible ,  testable , and  easy to maintain . Core Principles of Clean Architecture Dependency Inversion : High-level modules s...

How Does My .NET Core Application Build Once and Run Everywhere?

One of the most powerful features of .NET Core is its cross-platform nature. Unlike the traditional .NET Framework, which was limited to Windows, .NET Core allows you to build your application once and run it on Windows , Linux , or macOS . This makes it an excellent choice for modern, scalable, and portable applications. In this blog, we’ll explore how .NET Core achieves this, the underlying architecture, and how you can leverage it to make your applications truly cross-platform. Key Features of .NET Core for Cross-Platform Development Platform Independence : .NET Core Runtime is available for multiple platforms (Windows, Linux, macOS). Applications can run seamlessly without platform-specific adjustments. Build Once, Run Anywhere : Compile your code once and deploy it on any OS with minimal effort. Self-Contained Deployment : .NET Core apps can include the runtime in the deployment package, making them independent of the host system's installed runtime. Standardized Libraries ...