What is C#?
- C# is a modern, object-oriented programming language developed by Microsoft as part of the .NET framework. It is widely used for developing Windows applications, web applications, and more.
What is the difference between
==
and
.Equals()
in C#?
-
The
==
operator compares the values of two objects for equality, while the.Equals()
method checks if two objects are equal based on their contents. The.Equals()
method can be overridden in custom classes to provide custom comparison logic.
What is the difference between
String
and
StringBuilder
in C#?
-
String
is immutable, meaning its value cannot be changed after it's created.StringBuilder
, on the other hand, is mutable and allows for efficient manipulation of strings by appending, inserting, or removing characters without creating new objects.
Explain the concept of inheritance in C#.
- Inheritance is a mechanism in which a new class (derived class) is created based on an existing class (base class). The derived class inherits properties, methods, and other members from the base class and can also extend its functionality by adding new members or overriding existing ones.
What is an interface in C#?
- An interface is a reference type that defines a contract for classes to implement. It contains only method signatures, properties, events, or indexers. Classes that implement an interface must provide concrete implementations for all its members.
What is the difference between
IEnumerable
and
IQueryable
in C#?
-
IEnumerable
represents a collection of objects that can be enumerated using a foreach loop, suitable for in-memory collections.IQueryable
represents a query that can be executed against a data source, typically a database, and supports LINQ queries with deferred execution.
What is the purpose of the
using
statement in C#?
-
The
using
statement is used to ensure that resources like file streams, database connections, etc., are properly disposed of after they are no longer needed. It automatically calls theDispose()
method on objects that implement theIDisposable
interface when they go out of scope.
What is a delegate in C#?
- A delegate is a type that represents a reference to a method with a specific signature. It allows methods to be passed as parameters, stored in variables, and invoked dynamically. Delegates are commonly used in event handling and asynchronous programming.
Explain the difference between
ref
and
out
parameters in C#?
-
Both
ref
andout
parameters are used to pass arguments by reference instead of by value. However,ref
parameters must be initialized before passing them to a method, whereasout
parameters do not need to be initialized and are typically used for returning multiple values from a method.
What are the benefits of using LINQ in C#?
- LINQ (Language Integrated Query) provides a unified syntax for querying data from various data sources like collections, arrays, XML, SQL databases, etc. It offers compile-time type checking, intellisense support, and allows for writing more expressive and readable code compared to traditional approaches.
What are the different access modifiers available in C# and explain their visibility scopes?
-
C# provides five access modifiers:
public
,private
,protected
,internal
, andprotected internal
. -
public
: accessible from any code in the same assembly or another assembly. -
private
: accessible only within the containing type. -
protected
: accessible within the containing type and its derived types. -
internal
: accessible within the same assembly. -
protected internal
: accessible within the same assembly or from a derived class in another assembly.
What is the purpose of the
virtual
and
override
keywords in C#?
-
The
virtual
keyword is used to declare a method, property, or indexer that can be overridden in derived classes. -
The
override
keyword is used to provide a new implementation for a virtual member inherited from a base class.
What is the difference between
IEnumerable
and
IQueryable
in LINQ?
-
IEnumerable
represents a sequence of objects that can be enumerated, typically in-memory collections like arrays or lists. -
IQueryable
represents a query that can be executed against a data source, such as a database, and supports composition with other LINQ operators.
What is the purpose of the
async
and
await
keywords in C#?
-
The
async
keyword is used to declare an asynchronous method, which allows the method to run asynchronously without blocking the calling thread. -
The
await
keyword is used to pause the execution of an asynchronous method until the awaited task is complete, while allowing the calling thread to continue with other tasks.
Explain the difference between value types and reference types in C#?
- Value types store their actual values directly in memory, whereas reference types store references (pointers) to the memory location where the values are stored.
-
Examples of value types include
int
,float
, andstruct
. Examples of reference types includeclass
,interface
, andstring
.
What is boxing and unboxing in C#?
- Boxing is the process of converting a value type to a reference type, which involves wrapping the value inside an object.
- Unboxing is the process of extracting the value from a boxed object and converting it back to its original value type.
What is the purpose of the
using
directive in C#?
-
The
using
directive is used to import namespaces, allowing types defined in those namespaces to be used in the current code file without fully qualifying their names.
Explain the difference between
Array
and
ArrayList
in C#?
-
Array
is a fixed-size collection of elements of the same type, whileArrayList
is a dynamic collection that can grow or shrink in size and can hold elements of different types.
What is an anonymous type in C#?
- An anonymous type is a type without a name that is automatically generated by the compiler to encapsulate a set of properties or fields. They are often used in LINQ queries to represent projected data.
What is the purpose of the
params
keyword in C#?
-
The
params
keyword allows a method to accept a variable number of parameters of the same type, making it more flexible when calling the method with multiple arguments.
What is the
try
,
catch
,
finally
block used for in C#?
-
The
try
block is used to enclose code that might throw exceptions. Thecatch
block is used to handle specific types of exceptions that are thrown within thetry
block. Thefinally
block is used to specify code that should always be executed, regardless of whether an exception occurs or not.
What are the different types of exceptions in C#?
-
C# exceptions can be divided into two main categories: system exceptions
(derived from
System.Exception
) and application exceptions (derived fromApplicationException
). System exceptions includeNullReferenceException
,ArgumentException
,InvalidOperationException
, etc.
Explain the concept of polymorphism in C# with an example.
-
Polymorphism allows objects of different types to be treated as objects of
a common base type. This can be achieved through inheritance and method
overriding. For example, a
Circle
class and aRectangle
class can both inherit from a commonShape
class and override aDraw
method to provide different implementations.
What is the difference between
List<T>
and
Dictionary<TKey, TValue>
in C#?
-
List<T>
represents a dynamic collection of elements of a specified type, typically accessed by index.Dictionary<TKey, TValue>
represents a collection of key-value pairs, where each key is unique and used to access its associated value.
Aucun commentaire:
Enregistrer un commentaire