C# Interview Questions (part-3)

What is code access security (CAS)?

Code Access Security (CAS) serves as the designated security sandbox for the .NET framework, offering a controlled environment for executing code. Local applications typically possess full trust, granting them unrestricted privileges and capabilities. Conversely, .NET applications hosted within a browser have limited permissions and are restricted in their actions. However, CAS provides a range of flexible security settings that can be finely adjusted to meet specific requirements. This allows developers to define and enforce varying levels of security measures, ensuring a balance between functionality and protection. By leveraging CAS, developers can effectively tailor the security posture of their .NET applications, aligning it with the desired security policies and mitigating potential risks and vulnerabilities.

More on.... Code Access Security

Which is the best place to store connection string in .NET projects?

Web.config and App.config are the recommended locations for storing connection strings in the context of .NET applications. The Web.config file is typically utilized for web-based applications, while the App.config file is employed for Windows applications. These configuration files provide a centralized and structured approach to manage connection strings, allowing for easy modification and maintenance. By storing connection strings in these dedicated configuration files, developers can ensure secure and flexible management of database connections, promoting scalability and ease of deployment. Additionally, separating connection strings from the codebase enhances maintainability and facilitates configuration changes without requiring recompilation of the application.

What is strong-typing versus weak-typing?

  1. Strong-typing is compiler enforced declaration and maintenance of variable types such that you do not fall into the trap of performing operation and analysis on types which are incompatible with each other.
  2. Weak-typing allows ambiguities and maintenance of variable operations is more, so on the coder, opening up possibilities of creating difficult to trap errors.

Difference between Math.Floor() and Math.Truncate()?

Mat.floor() will always round down and Math.Truncate rounds towards zero.

Math.Floor(2.5) = 2 Math.Truncate(2.5) = 2
Math.Floor(-2.5) = -3 Math.Truncate(-2.5) = -2

What are pointer types in C#?

In an unsafe context, a type may be a pointer type.

type* identifier;

The following types may be a pointer type:

  1. sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double, decimal, or bool.

  2. Any enum type.

  3. Any pointer type.

  4. Any user-defined struct type that contains fields of unmanaged types only.

What is the smallest unit of execution in .NET?

An assembly is the smallest unit of execution in .Net

How do you convert a value-type to a reference-type?

With the help of Boxing and UnBoxing you can convert any value type to reference type.

int i = 10; Object obj = (Object)i;
int j=(int)obj;

What does the keyword "virtual" declare for a method or property?

The "virtual" keyword serves as a modifier that enables the modification of method, property, indexer, or event declarations, granting them the capability to be overridden in a derived class. In the absence of the "virtual" keyword, a method cannot be overridden in a derived class by default, unless it is explicitly declared as virtual or abstract. This mechanism ensures a controlled and deliberate approach to defining the extensibility and polymorphic behavior of methods in object-oriented programming. By marking a member as virtual, developers provide an explicit indication that the member can be overridden in a derived class, promoting flexibility and facilitating the implementation of specialized functionality in subclasses.

Is XML case-sensitive?

Yes - XML is case sensitive

What is the Global.asax used for?

The Global.asax file, recognized as the ASP.NET application file, is an optional component within an ASP.NET application. It functions as a repository for code responsible for handling application-level and session-level events triggered by ASP.NET or HTTP modules. This file provides a centralized location for developers to define and implement custom logic and event handlers to respond to various events throughout the application's lifecycle. By leveraging the Global.asax file, developers gain the ability to tailor the behavior and functionality of the application to specific events, enhancing the overall user experience and facilitating seamless integration with the underlying ASP.NET framework and associated modules.

What is Multi-tasking?

It is a feature of modern operating systems with which we can run multiple programs at same time example Word, Excel etc.

How can you reference current thread of the method?

The "Thread.CurrentThread" refers to the thread that is currently executing within the context of a method. This property allows developers to obtain a reference to the thread itself and access its properties and methods. Notably, "Thread.getCurrentThread()" is a static method, implying that it operates on the class itself rather than an instance of the Thread class. By invoking this static method, developers can retrieve information and perform actions related to the current thread, such as accessing thread-specific data or managing thread synchronization. Understanding the distinction between the "Thread.CurrentThread" property and the "Thread.getCurrentThread()" static method is crucial for effective thread management and synchronization within multi-threaded applications.

How to escape curly brackets in a format string in C#?

In order to output "{" in string.Format you have to escape it like this {{ and to output a "}" you can use }}.

string str = "1, 2, 3"; string output = String.Format(" foo {{{0}}}", str);

Is overriding of a function possible in the same class?

No. It is not possible, the override modifier is used to extend the base class method not for hiding method definitions in the current class.

How do you prevent a class from being inherited?

In C#, the "sealed" keyword serves as a mechanism to restrict the inheritance of a class, preventing it from being further derived by other classes. On the other hand, in VB.NET, the equivalent keyword used is "NotInheritable". These keywords signify that a class is intentionally designed to be non-inheritable, safeguarding its integrity and ensuring that it remains as a final implementation without any further extension. By employing the "sealed" keyword in C# or the "NotInheritable" keyword in VB.NET, developers can enforce design constraints and maintain the intended behavior of the class, promoting code stability and preventing unintended modifications through inheritance.

Can a private virtual method be overridden?

No. You can't even declare private virtual methods. Because private methods can ONLY be accessed from the class defining them, hence a private virtual method would be useless.

List the types of Constructors in C#?

Constructors can be divided into 5 types:

  1. Default constructor
  2. Parameterized constructor
  3. Instance constructor
  4. Static constructor
  5. Private constructor

What is the difference between bool and Boolean types in C#?

Both are same, bool is an alias for System.Boolean.

You can assign a Boolean value to a bool variable.

Can you store multiple data types in System.Array?

Array store only single datatype. If you want to store different types, use System.Collections.ArrayList or object[]

Array is reference type or value type ?

Array in .net is of reference type.

All array types are implicitly derived from System.Array, which itself is derived from System.Object . This means that all arrays are always reference types

What are different types of an Array ?

  1. Single Dimension Arrays
  2. Multi-Dimensional Arrays
  3. Jagged Arrays

Can finally bock have return statement?

You can't return from finally block. You will get compiler error:

"Control cannot leave the body of a finally clause"

It is a compile-time error for a return statement to occur in a finally block.

Does C# have a throws clause?

No, because there are no checked exceptions in C#.

Can multiple catch blocks be executed in C#?

No, When handling exceptions in C#, it's important to note that only one exception will be thrown at a time. However, you can have multiple catch blocks associated with a try block to handle different types of exceptions. It's crucial to understand that once an exception is caught and the corresponding catch block is executed, control is then transferred to the finally block, ensuring that any necessary cleanup or finalization operations are performed. After the execution of the finally block, the code that follows will be executed. It's important to emphasize that the program does not return to the try block, preventing the occurrence of a second exception within the same exception handling block. This ensures a controlled and structured approach to exception handling and prevents the cascading of multiple exceptions.

Will finally block get executed if the exception had not occurred?

The code inside a finally block will get executed regardless of whether or not there is an exception. Also the catch block is only executed if a managed exception is thrown within the associated try block.

try { return "something"; } finally { // This will always be invoked }