Skip to main content

C# : Interview questions (6-10)


 Questions :

  • What is the difference between stack and heap memory?
  • How do you declare a variable in C#?
  • Explain the use of the "var" keyword in C#.
  • What are the different access modifiers in C#?
  • How does C# support polymorphism?

Answers :

Difference between Stack and Heap Memory

Stack and heap are two types of memory used by computer programs, including those written in C#. Here's how they differ:

  1. Stack Memory:

    • Stack memory is used for storing local variables and function call information.
    • It operates in a Last In, First Out (LIFO) manner, where the last item added is the first to be removed.
    • Memory allocation and deallocation on the stack are fast because it involves adjusting the stack pointer.
    • Stack memory is limited in size and is typically smaller than heap memory.
    • Stack memory is automatically managed by the compiler or runtime environment.
  2. Heap Memory:

    • Heap memory is used for dynamic memory allocation, where memory is allocated and deallocated explicitly by the programmer.
    • It operates in a more flexible manner, allowing memory to be allocated and deallocated in any order.
    • Memory allocation and deallocation on the heap can be slower than on the stack because it involves searching for suitable free memory blocks.
    • Heap memory is typically larger than stack memory and can grow dynamically as needed.
    • Heap memory management is the responsibility of the programmer, who must manually allocate and deallocate memory using functions like new and delete (or in C#, using constructs like new and garbage collection).

Declaring a Variable in C#

In C#, you can declare a variable using the following syntax:

<data-type> <variable-name>;
Here, <data-type> refers to the type of data the variable will hold, such as int, float, string, etc., and <variable-name> is the name you give to the variable. For example
int age;
string name;
float salary;

These statements declare variables age, name, and salary of types int, string, and float, respectively.

Use of the "var" Keyword in C#

The "var" keyword in C# is used for implicit typing, allowing the compiler to infer the type of a variable based on the value assigned to it. Here's how it works

var variableName = value;
Instead of explicitly specifying the data type of the variable, you can use "var", and the compiler will determine the appropriate data type based on the value assigned to the variable. For example
var age = 25; // Compiler infers int type
var name = "John"; // Compiler infers string type
var salary = 50000.50f; // Compiler infers float type

The "var" keyword is especially useful when the type of the variable is obvious from the initialization expression, or when dealing with complex types like LINQ queries or anonymous types.

Different Access Modifiers in C#

C# provides several access modifiers to control the accessibility of classes, methods, properties, and other members within a program. The main access modifiers in C# are:

  1. public: The public access modifier allows members to be accessed from any other class or assembly.

  2. private: The private access modifier restricts the accessibility of members to the containing class only. They cannot be accessed from outside the class.

  3. protected: The protected access modifier allows members to be accessed within the containing class and its derived classes.

  4. internal: The internal access modifier restricts access to members to the current assembly. Members marked as internal can be accessed from any class within the same assembly but not from outside assemblies.

  5. protected internal: The protected internal access modifier combines the behavior of protected and internal. It allows members to be accessed within the same assembly or from derived classes, even if they are in different assemblies.

Support for Polymorphism in C#

C# supports polymorphism through various mechanisms, including inheritance, interfaces, and method overriding. Polymorphism allows objects of different types to be treated as objects of a common base type, simplifying code and enabling flexibility. Here's how C# supports polymorphism:

  1. Inheritance: C# allows classes to inherit from other classes, inheriting their members and behaviors. This enables polymorphic behavior, where objects of derived classes can be treated as objects of their base class.

  2. Interfaces: Interfaces define a contract that classes can implement. They enable polymorphic behavior by allowing objects of different classes to be treated uniformly based on their shared interface.

  3. Method Overriding: C# allows derived classes to override base class methods, providing their own implementation. This enables polymorphic behavior, where the same method call can exhibit different behavior depending on the actual type of the object.

By leveraging these features, C# enables developers to write code that is flexible, extensible, and easier to maintain, promoting the principles of polymorphism in object-oriented programming.

C# : Interview questions (11-15)

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