CodeBlog.xyz

C# – 70 intermediate questions

September 6, 2023 | by Meir Achildiev

C# questions

intermediate C# questions:

  1. What is Reflection in C#?
    • Reflection in C# is the ability of a program to inspect its own structure, particularly through types. It allows you to dynamically create instances of types, inspect the fields, properties and methods of types, and invoke methods.
  2. What is Boxing and Unboxing in C#?
    • Boxing is the process of converting a value type to the type object or to any interface type implemented by this value type. Unboxing is the process of converting the type object to value types.
  3. What is the difference between Finalize and Dispose method in C#?
    • Finalize is used for cleaning unmanaged resources and it is called automatically by the garbage collector before the object is destroyed. Dispose is used for cleaning both managed and unmanaged resources and it has to be called explicitly by a programmer.
  4. What is the difference between is and as operators in C#?
    • The is operator is used to check the compatibility of an object with a given type, and it returns the result as Boolean. The as operator is used for casting of object to a type or a class.
  5. What is the Global Assembly Cache (GAC) in C#?
    • The Global Assembly Cache (GAC) is a machine-wide CLI assembly cache for the Common Language Infrastructure (CLI) in Microsoft’s .NET Framework. The approach of having a specially controlled central repository addresses the shared library concept and helps to prevent “DLL hell”.
  6. What is the difference between out and ref parameters in C#?
    • The out keyword causes arguments to be passed by reference but doesn’t require the initial value. The ref keyword causes arguments to be passed by reference and requires the initial value.
  7. What is the volatile keyword in C#?
    • The volatile keyword in C# is used to indicate that a field might be modified by multiple threads that are executing at the same time. Fields that are declared volatile are not subject to compiler optimizations that assume access by a single thread.
  8. What are sealed classes in C#?
    • A sealed class in C# is a class that cannot be inherited. Sealed classes limit the inheritance feature of object oriented programming. Once a class is defined as a sealed class, this class cannot be inherited.
  9. What is the ?: operator in C#?
    • The ?: operator in C# is a conditional operator also known as the ternary conditional operator. This operator consists of three operands and is used to evaluate Boolean expressions.
  10. What is the difference between const and readonly in C#?
    • const is a keyword which is used to declare a constant field or a constant local. const fields or locals must be assigned a value at the time of declaration and after that they cannot be modified. On the other hand, readonly keyword is different from const keyword. readonly is a modifier that you can use on fields. When a field declaration includes a readonly modifier, assignments to the fields introduced by the declaration can only occur as part of the declaration or in a constructor in the same class.
  11. What is the difference between struct and class in C#?
    • In C#, both struct and class are used to handle complex data, but they are different in several aspects. struct is a value type while class is a reference type. struct does not support inheritance, while class does.
  12. What is Automatic Properties in C#?
    • Automatic properties are a shortcut where the compiler creates the backing field for you. You create the property as if it was only a property, but when you compile, the compiler creates a private, anonymous backing field that is not accessible except through the property’s get and set accessors.
  13. What is the difference between String and StringBuilder in C#?
    • String is an immutable type, which means that any operation that appears to change the value of a String object actually creates a new string. StringBuilder, on the other hand, is mutable, and is a better choice when dealing with string manipulation scenarios where the operation needs to be performed multiple times.
  14. What is Operator Overloading in C#?
    • Operator overloading is a compile-time polymorphism in which the operator is overloaded to provide the special meaning to the user-defined data type. Operator overloading is used to overload or redefines most of the operators available in C#. It is used to perform operation on user-defined data type.
  15. What is Partial Class in C#?
    • A partial class or struct may be split into multiple files. The implementation of the class can be distributed into multiple text files. The file name does not impact the name of the class, and every text file which contains a part of the class definition must use the keyword partial.
  16. What are the different types of constructors in C#?
    • In C#, there are three types of constructors: default constructor, parameterized constructor and copy constructor.
  17. What is the use of params keyword in C#?
    • The params keyword in C# allows for a method parameter to take a variable number of arguments of a specified type. It’s used in the method signature to declare that the method takes a variable number of arguments.
  18. What is Polymorphism in C#?
    • Polymorphism is a feature of object-oriented programming that allows a class to have multiple forms, i.e., classes can have methods with the same name but different implementations. In C#, polymorphism is implemented via method overloading, method overriding, and inheritance.
  19. What are Attributes in C#?
    • Attributes provide a way of associating declarative information with code in .NET. Once an attribute is associated with a program entity, the attribute can be queried at runtime using a technique called reflection.
  20. What is an Extension Method in C#?
    • Extension methods enable you to add methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type.
  21. What is the difference between throw and throw ex in C#?
    • throw re-throws the exception that was caught, preserving the stack trace. throw ex throws the exception again, but resets the call stack to the current catch block.
  22. What are the different types of collections in C#?
    • Collections in C# are a group of objects. Examples include List<T>, Dictionary<K, V>, HashSet<T>, Queue<T>, and Stack<T>.
  23. What is the difference between System.Array.CopyTo() and System.Array.Clone()?
    • CopyTo() method copies the elements into another pre-existing array starting from a given index. Clone() method returns a new array (a shallow copy) containing all the elements.
  24. What is the purpose of the using statement in C#?
    • The using statement in
    C# is used for dealing with unmanaged resources such as files, network resources, database connections, etc. It is used to ensure that the Dispose method will be called even if an exception occurs while you are calling methods on the object.
  25. What are Namespaces in C#?
    • A namespace is a declarative region that provides a scope to the identifiers (the names of types, functions, variables, etc) inside it. Namespaces are used to organize code into logical groups and to prevent name collisions that can occur especially when your code base includes multiple libraries.
  26. What is Serialization in C#?
    • Serialization is the process of converting an object into a stream of bytes to store the object or transmit it to memory, a database, or a file. Its main purpose is to save the state of an object in order to be able to recreate it when needed.
  27. What is Deserialization in C#?
    • Deserialization is the reverse of Serialization, it is used to convert bytes of stream data to an object.
  28. What is IEnumerable and IEnumerator in C#?
    • IEnumerable and IEnumerator are interfaces available in C#. IEnumerable contains a single method, GetEnumerator, which returns an IEnumerator. IEnumerator provides the ability to iterate through the collection by exposing a Current property and MoveNext and Reset methods.
  29. What is the yield keyword in C#?
    • The yield keyword is used in an iterator block to provide a value to the enumerator object, or to signal the end of iteration. It can only appear inside an iterator block, which can be implemented as a method, a get accessor, or in a foreach statement.
  30. What is the difference between IEnumerable and IQueryable in C#?
    • IEnumerable is best suitable for working with in-memory collection. IQueryable is best suitable for remote data source, like a database or web service. IQueryable is a very powerful feature that enables a variety of interesting deferred execution scenarios (like paging and composition based queries).
  31. What is the purpose of base keyword in C#?
    • The base keyword is used to access members of the base class from the derived class.
  32. What is the difference between virtual and abstract methods in C#?
    • A virtual method is a method that can be redefined in derived classes. An abstract method is a method declared in an abstract class, and does not have implementation in the same class.
  33. What is Multithreading in C#?
    • Multithreading is a technique which allows a single process to have multiple threads running concurrently within the same process unit.
  34. What is Thread Pooling in C#?
    • Thread pooling is a form of multithreading that allows a pool of worker threads to be created and used to perform relatively short-lived asynchronous I/O bound tasks.
  35. What is Asynchronous Programming in C#?
    • Asynchronous programming is a form of parallel programming that allows a unit of work to run separately from the primary application thread. It’s commonly used for tasks such as accessing the web, file access, database operations, etc.
  36. What is the difference between Task and Thread in C#?
    • Thread is a lower level way of doing multi-threading in .NET, while Task is more high level and is based on the thread pool. Task also supports returning a result and is easier to use with async and await keywords.
  37. What is the lock statement in C#?
    • The lock keyword in C# ensures that one thread does not enter a critical section of code while another thread is in the critical section. If another thread attempts to enter a locked code, it will wait (block) until the object is released.
  38. What is Exception Handling in C#?
    • Exception handling in C# is a process in .NET framework to handle runtime errors. It is done by using try-catch-finally blocks.
  39. What is a Delegate in C#?
    • A delegate in C# is similar to a function pointer in C or C++. It’s a reference type data type that defines the signature (return type and parameters) of a method.
  40. What is a Lambda expression in C#?
    • A lambda expression is an anonymous function that you can use to create delegates or expression tree types. Lambda expressions use the lambda operator =>, read as “goes to”.
  41. What is LINQ in C#?
    • Language-Integrated Query (LINQ) is the name for a set of technologies based on the integration of query capabilities directly into the C# language.
  42. What is the difference between IEnumerable and IList in C#?
    • IEnumerable is an interface that defines one method GetEnumerator which returns an IEnumerator interface, this allows readonly access to a collection. IList is more flexible and allows adding, removing and updating items.
  43. What are Indexers in C#?
    • Indexers allow instances of a class or struct to be indexed just like arrays. They resemble properties except that their accessors take parameters.
  44. What is the dynamic keyword in C#?
    • The dynamic keyword allows the type checking of objects to be deferred until runtime as opposed to compile-time.
  45. What is the difference between dynamic and var in C#?
    • The var keyword instructs the compiler to infer the type of the variable from the expression on the right side of the initialization statement. The dynamic keyword tells the compiler to turn off compile-time checking.
  46. What are Generics in C#?
    • Generics allow you to define type-safe data structures, without committing to actual data types. This leads to a high level of code reuse.
  47. What are Nullable Types in C#?
    • Nullable types are instances of the System.Nullable struct. A nullable type can represent the normal range of values for its underlying value type, plus an additional null value.
  48. What is Object Pooling in C#?
    • Object pooling is a software design pattern that uses a set of initialized objects kept ready to use, rather than allocating and destroying them on demand.
  49. What is an Anonymous Method in C#?
    • An anonymous method is an inline unnamed method in the code itself. It is created using the delegate keyword and doesn’t need to have a name.
  50. What is a Partial Method in C#?
    • A partial method has its signature defined in one part of a partial class, struct, or interface and its implementation defined in another part.
  51. What is the stackalloc keyword in C#?
    • The stackalloc keyword is used in an unsafe code context to allocate a block of memory on the stack.
  52. What is the difference between Equals() and == in C#?
    • Equals() is a method, and == is an operator. Equals() method is used to compare the contents of two objects, and == operator is used to compare the references of two objects.
  53. What is Deadlock in C#?
    • A deadlock is a situation where two or more threads are blocked forever, waiting for each other to release resources.
  54. What are Events in C#?
    • Events in C# are a way for a class to provide notifications to clients of that class when some interesting thing happens to an object. They are a special kind of multicast delegate that can only be invoked from within the class or struct where they are declared.
  55. What is Tuple in C#?
    • Tuple is a data structure that contains a sequence and can hold heterogeneous data types. Tuples can be used where you want to return multiple values from a method in a single parameter.
  56. What are Pointers in C#?
    • A pointer is a variable that holds the memory address of another type. C# supports pointers in a limited extent.
  57. What is the difference between continue and break statements in C#?
    • continue statement passes control to the next iteration of the enclosing iteration statement in which it appears. break statement terminates the closest enclosing loop or switch statement.
  58. What is Garbage Collection in C#?
    • Garbage Collection in C# is a process of reclaiming the unused memory by the idle objects in an application to free up memory space.
  59. What are Value Types and Reference Types in C#?
    • Value types directly contain their data, and instances of value types are either allocated on the stack or allocated inline in a structure. Reference types store a reference to the value’s memory address and are allocated on the heap.
  60. What is the use of this keyword in C#?
    • The this keyword refers to the current instance of the class. It is also used as a modifier of the first parameter of an extension method.
  61. What is Enum in C#?
    • An enum (enumeration) is a value type with a set of related named constants often referred to as an enumerator list.
  62. What is the difference between struct and class in C#?
    • The struct is a value type in C#, and it inherits from System.ValueType. On the other hand, a class is a reference type. While instances of a class are created on the heap, instances of a struct are created on the stack.
  63. What is the difference between is and as operators in C#?
    • The is operator is used to check the compatibility of an object with a given type, and it returns the result as Boolean. The as operator is used for casting of object to a type or a class.
  64. What is Polymorphism in C#?
    • Polymorphism means one name, many forms. It’s the ability of a variable, function or object to take on multiple forms. In C#, polymorphism is implemented with inheritance, interface and method overriding.
  65. What is Encapsulation in C#?
    • Encapsulation is defined as the wrapping up of data under a single unit. It is the mechanism that binds together code and the data it manipulates.
  66. What is the Dispose method in C#?
    • The Dispose method is used for releasing unmanaged resources like files, database connections, network resources, etc. It’s a part of the IDisposable interface.
  67. What is Boxing and Unboxing in C#?
    • Boxing is the process of converting a value type data type to the object or to any interface data type which is implemented by this value type. Unboxing extracts the value type from the object or any implemented interface type.
  68. What is Reflection in C#?
    • Reflection in C# is used to retrieve metadata about types at runtime. It allows you to inspect a type’s members, create an instance of a type, invoke methods, and get or set property or field values.
  69. What is the Global Assembly Cache (GAC) in C#?
    • The Global Assembly Cache (GAC) is a machine-wide CLI assembly cache for the Common Language Infrastructure (CLI) in Microsoft’s .NET Framework. The approach of having a specially controlled central repository addresses the shared library concept and helps to avoid pitfalls of other solutions that lead to drawbacks like DLL hell.
  70. What is the async keyword in C#?
    • The async keyword is used to specify that a method, lambda expression, or anonymous method is asynchronous. Asynchronous methods don’t require multithreading because an asynchronous method doesn’t run on its own thread.

RELATED POSTS

View all

view all