- What is C#?
C# is a modern, general-purpose programming language developed by Microsoft. It is widely used for building various types of applications on the .NET framework.
- What is the .NET framework?
The .NET framework is a software development platform that provides a runtime environment and a set of libraries for developing and running applications. It supports multiple programming languages, including C#.
- What is the difference between C# and .NET?
C# is a programming language, while .NET is a framework that provides a runtime and libraries for building applications. C# is one of the languages used to develop applications on the .NET framework.
- Explain the concept of object-oriented programming (OOP).
Object-oriented programming is a programming paradigm that organizes software design around objects that represent real-world entities. It emphasizes concepts such as encapsulation, inheritance, and polymorphism.
- What are the main features of C#?
C# supports features like type safety, garbage collection, memory management, exception handling, and support for object-oriented programming concepts like classes, interfaces, and inheritance.
- What is the difference between a class and an object?
A class is a blueprint or template for creating objects, while an object is an instance of a class. Classes define the properties and behaviors that objects of that class can have.
- Describe the difference between value types and reference types in C#.
Value types store their actual data, such as integers and booleans, directly in memory, while reference types store a reference to the data in memory. Value types are stored on the stack, while reference types are stored on the heap.
- What is the purpose of the “using” statement in C#?
The “using” statement is used for automatic resource management. It ensures that objects that implement the IDisposable interface are properly disposed of, freeing up system resources.
- What is the role of the Common Language Runtime (CLR) in .NET?
The Common Language Runtime (CLR) is the execution environment provided by the .NET framework. It manages memory, handles exceptions, and provides other services required by .NET applications.
- What are the access modifiers in C#?
C# provides access modifiers like public, private, protected, and internal to control the visibility and accessibility of classes, methods, and other members.
- Explain the “this” keyword in C#.
The “this” keyword refers to the current instance of a class and is used to access members of the current object. It is often used to disambiguate between instance variables and parameters with the same name.
- What is the purpose of the “static” keyword in C#?
The “static” keyword is used to define members (variables, methods) that belong to a type itself rather than to instances of the type. Static members can be accessed without creating an instance of the class.
- What is the difference between an abstract class and an interface?
An abstract class can contain both method definitions and implementations, while an interface can only contain method signatures. A class can inherit from multiple interfaces but can only inherit from one abstract class.
- Explain the concept of polymorphism in C#.
Polymorphism refers to the ability of objects of different types to be treated as objects of a common base type. It allows methods to be overridden in derived classes and enables dynamic binding at runtime.
- What is the purpose of the “virtual” keyword in C#?
The “virtual” keyword is used to define a method in a base class that can be overridden in derived classes. It allows for polymorphic behavior when
calling methods on objects of different derived classes.
- What are the different types of inheritance in C#?
C# supports single inheritance, where a class can inherit from only one base class. However, multiple interfaces can be implemented by a single class, allowing for multiple inheritance of behaviors.
- What is the purpose of the “sealed” keyword in C#?
The “sealed” keyword is used to prevent a class from being inherited. It is applied to a class or a method to indicate that it cannot be overridden in derived classes.
- How does exception handling work in C#?
Exception handling in C# involves using try-catch-finally blocks. Code that may throw exceptions is placed in the try block, and any caught exceptions are handled in the catch block. The finally block is used for cleanup actions.
- What is the difference between a stack and a heap in memory management?
In C#, the stack is used to store value types and method call information, while the heap is used to store reference types and dynamically allocated objects. The stack is managed automatically by the runtime, while the heap requires manual memory management.
- Explain the difference between “==” and “Equals()” in C#.
The “==” operator is used to compare the equality of two objects or values, whereas the “Equals()” method is used to check for equality as well but can be overridden by classes to provide custom comparison logic.
- What are delegates and events in C#?
Delegates are a type-safe function pointer that holds references to methods. They are often used to implement callback mechanisms. Events are a special type of delegate used for implementing the observer pattern.
- What is LINQ (Language Integrated Query)?
LINQ is a set of language features in C# that allow querying data from different sources using a unified syntax. It enables developers to query and manipulate data from collections, databases, XML, and more.
- Explain the difference between “IEnumerable” and “IQueryable”.
“IEnumerable” represents a sequence of objects that can be enumerated, whereas “IQueryable” represents a queryable sequence of objects that can be evaluated against a data source. “IQueryable” is typically used in database querying scenarios.
- What is the purpose of the “async” and “await” keywords in C#?
The “async” and “await” keywords are used to write asynchronous code in a more readable and structured manner. They allow methods to be marked as asynchronous and enable non-blocking execution.
- What is the difference between a value type and a nullable type?
A value type holds the value directly, while a nullable type can hold either a value or a null reference. Nullable types are used when a value type needs to allow for nullability, such as a nullable integer.
- How can you handle multi-threading in C#?
C# provides features like threads, tasks, and the Task Parallel Library (TPL) to handle multi-threading. These features allow you to execute code concurrently, synchronize access to shared resources, and handle synchronization and coordination between threads.
- Explain the concept of garbage collection in .NET.
Garbage collection is the automatic memory management mechanism in .NET that tracks and reclaims memory that is no longer in use. It frees developers from explicitly deallocating memory and helps prevent memory leaks.
- What is the purpose of the “yield” keyword in C#?
The “yield” keyword is used in iterator methods to create an iterator block. It allows you to define custom iteration logic without the need to create a separate class that implements the IEnumerator interface.
- What is a lambda expression in C#?
A lambda expression is an anonymous function that can be used to create delegates or expression trees. It provides a concise syntax for defining inline functions without explicitly declaring a separate method.
- Explain the difference between a struct and a class in C#.
A struct is a value type that is typically used for small, lightweight objects, whereas a class is a reference type used for more complex objects. Structs are passed by value, while classes are passed by reference.
- What is the purpose of the “using” directive in C#?
The “using” directive is used to include namespaces in your code file. It allows you to use types from the specified namespace without fully qualifying their names.
- How can you handle file I/O in C#?
C# provides classes like FileStream, StreamReader, and StreamWriter for reading from and writing to files. These classes offer methods for various file I/O operations.
- What is the purpose of the “StringBuilder” class in C#?
The “StringBuilder” class is used for efficient string manipulation. It provides methods to append, replace, or insert characters into a string without creating multiple string objects.
- Explain the concept of boxing and unboxing in C#.
Boxing is the process of converting a value type to a reference type, while unboxing is the reverse process. Boxing allows value types to be treated as objects, but it incurs performance overhead.
- What is the purpose of the “var” keyword in C#?
The “var” keyword is used to declare implicitly typed variables. The compiler infers the type of the variable based on the expression used to initialize it.
- How can you implement data validation in C#?
Data validation can be implemented by using conditional statements, regular expressions, or by leveraging built-in validation attributes provided by .NET, such as the Required attribute or the Range attribute.
- What are attributes in C#?
Attributes provide a way to associate metadata with types, methods, properties, and other program elements. They can be used for various purposes, such as adding additional information, applying behaviors, or enabling reflection.
- Explain the difference between a shallow copy and a deep copy in C#.
A shallow copy copies the references of an object, while a deep copy creates a new object and copies the values of the original object’s fields or properties. A deep copy creates a completely independent object.
- What is the purpose of the “volatile” keyword in C#?
The “volatile” keyword is used to indicate that a variable may be modified by multiple threads, and therefore, the compiler should not perform certain optimizations that could lead to incorrect behavior in multi-threaded scenarios.
- How can you handle errors and exceptions in C#?
Errors and exceptions can be handled using try-catch blocks. The code that may throw exceptions is placed in the try block, and any caught exceptions are handled in the catch block. Finally, blocks can be used for cleanup actions.
- What is the purpose of the “params” keyword in C#?
The “params” keyword allows a method to accept a variable number of arguments of a specified type. It simplifies method calls by allowing the caller to provide a comma-separated list of arguments.
- Explain the concept of reflection in .NET.
Reflection is the ability of a program to examine and modify its own structure, such as types, properties, methods, and attributes, at runtime. It provides mechanisms to discover and interact with code dynamically.
- What is the purpose of the “nameof” operator in C#?
The “nameof” operator returns the name of a variable, type, or member as a string. It is useful for avoiding hard-coded strings when working with names of program elements.
- How can you implement encryption and decryption in C#?
C# provides cryptographic classes in the System.Security.Cryptography namespace for implementing encryption and decryption algorithms. These classes allow you to encrypt and decrypt data using various encryption algorithms.
- Explain the concept of dependency injection in C#.
Dependency injection is a design pattern that allows objects to depend on abstractions rather than concrete implementations. It provides flexibility, testability, and loose coupling between components.
- What is the purpose of the “Nullable” type in C#?
The “Nullable” type allows value types to have a null value. It is useful when you need to represent the absence of a value for value types that don’t normally allow nulls, such as integers or booleans.
- How can you work with XML in C#?
C# provides classes like XmlDocument, XmlReader, and XmlWriter for working with XML data. These classes allow you to read, write, and manipulate XML documents.
- Explain the concept of serialization and deserialization in C#.
Serialization is the process of converting an object into a format that can be stored or transmitted, such as XML or binary. Deserialization is the reverse process of reconstructing the object from the serialized data.
- What is the purpose of the “lock” keyword in C#?
The “lock” keyword is used to ensure exclusive access to a shared resource in multi-threaded scenarios. It allows only one thread at a time to execute a specific section of code, preventing conflicts.
- How can you optimize the performance of your C# code?
Performance optimization can be achieved by using efficient algorithms and data structures, minimizing object allocations, reducing unnecessary memory access, and utilizing multi-threading or asynchronous programming where applicable.
These short answers provide a quick overview of the key points for each question. However, it’s essential to delve deeper into each topic and gain a solid understanding before an interview.