TOP-10 C Sharp Questions for Experienced Developers - ByteScout
  • Home
  • /
  • Blog
  • /
  • TOP-10 C Sharp Questions for Experienced Developers

TOP-10 C Sharp Questions for Experienced Developers

C Sharp for Experienced

1. Enlist the characteristics of the Unsafe Code.

Characteristics of Unsafe Code are:

  • Methods, types, and code blocks can all be classified as unsafe.
  • In some circumstances, unsafe code might improve the efficiency of an application by omitting array bounds checks.
  • To invoke native methods that require pointers, unsafe code is required.
  • When you use unsafe code, you put your security and stability at risk.
  • The program must be compiled with /unsafe to compile unsafe code.

2. Give the use of Dynamic, in C# and how can it be differentiated with Reflection?

The keyword dynamic was first introduced in.NET 4.0. There are two types of computer programming languages: highly typed and dynamically typed. All type checks occur at compile time in strongly typed, but all types of checks happen at run time in dynamic types.

When working with Microsoft Office, the dynamic keyword is very useful. During runtime, both reflection and dynamic are used to work on an object. However, there are some distinctions:

  • Internally, Dynamic makes advantage of reflection.
  • Both public and private members of an object can be accessed by reflection. However, the dynamic can only access an object’s public members.

3. Explain the role of lambda expression in C#.

An anonymous function that can be used to build delegates or expression tree types is a lambda expression. You can build local functions that can be supplied as arguments or returned as the value of function calls using lambda expressions. Lambda expressions come in handy for writing LINQ query expressions in particular.

4. Throw some light on object-oriented concepts.

C# is an object-oriented programming language that supports four different Object-oriented Programming principles.

Encapsulation

This is a term that refers to the process of tying together code and data and keeping it safe from alteration by other programs and classes. It’s a container that blocks code and data from being accessed by a program specified outside of it.

Abstraction

To promote efficiency and security within the program, this object-oriented notion protects anything other than the relevant data about any produced object.

Inheritance

When one object uses the properties of another, this is known as inheritance.

Polymorphism

It is a feature that allows one interface to serve as the base class for additional interfaces. The phrase “one interface, multiple actions” is frequently used to describe this concept.

5. Explain the difference between break and continue statements in C#.

The differences are as follows:

Break Continue
Both switch and loop (for, while, and do-while ) statements can employ break statements.     Just the loop (for, while, do) statements can employ continue statements. Just the loop (for, while, do) statements can employ continue statements.
The switch or loop statements are terminated when the break statement is executed, and the program ends suddenly.  A continue statement cannot be used to end a loop; it just continues the loop to the next iteration level without completing the immediate next step.
When the compiler sees a break statement and exits the inner loop, the loop or switch quits instantly. The next loop iteration is triggered by a continuation placed inside a nested loop within a switch.

6. How would you go over the use of C# using’  statements?

The utilizing statement is used to manage the use of one or more resources within the program. Resources are consumed and released in a continual cycle. This statement’s primary purpose is to automatically manage and release unused resources. When you’re done creating the object that’s utilizing the resource, make sure to contact the object’s dispose function to release the resources it’s utilizing; this is where using statements come in handy.

For example:

 using  (MyResource abc = new
 MyResource())
 {
 abc.program();
 }
Gets translated to,
 MyResource abc= new MyResource();
 try
 {
 myRes.program();
 }
 finally
 {
  / / Check for a null resource;
  if (abc! = null)
      / / Call the object’s Dispose
  method.
                 ((IDisposable)abc).Dispose();
}

7. What will be the output of the following code snippet?

 Using System;
public class Program
{
public static void Main(string[] args)
{
int val = (byte)+(char)-(int)+(long)-2 ;
Console.WriteLine(val) ;
}
}
  1. a) Error
  2. b) -2
  3. c) 2
  4. d) 0

 

Answer: Option: c)

8. What’s the need for Reflection in C#?

A computer program’s structure and behavior can be monitored and modified using reflection. It’s a way to look at the structure of assemblies while they’re running (classes, resources, methods). At run-time, reflection is the ability to obtain relevant data, metadata, and program details (assemblies). To conduct reflections in C#, we must include the System.Reflection namespace.

Take a look at the C# programs below, which will yield some meta information.

 public class MyClass
 {
public virtual int Add(int numb1, int
 numb2)
{
return numb1 + numb2;
}
public virtual int Subtract(int numb1, int
 numb2)
{
return numb1 – numb2;
}
 }
 static void Main(string[] args)
 {
MyClass oMyClass = new MyClass();
//Type information.
Type oMyType = oMyClass.GetType();
//Method information.
MethodInfo oMyMethodInfo =
oMyType.GetMethod(“Subtract”);
Console.WriteLine(“nType information:” +
oMyType.FullName);
   Console.WriteLine(“nMethod info:” +
oMyMethodInfo.Name);
Console.Read();
}

9. In C#, how do you define exception handling?

A raised problem that may occur during the execution of the program is referred to as an exception. When an exception is raised, handling exceptions provides a straightforward mechanism to send control back to the program. Exceptions in C# are handled using four keywords: try, catch, finally, and throw.

Try

A raised exception identifies a specific section of code that has to be treated. There is no limit to how many catch blocks you can employ in your application to deal with various types of exceptions.

Catch

Within this catch block, you can handle the raised exception. You can specify the measures you wish to take to resolve the mistake, or you can simply ignore it by having the code suppress it.

Finally

Regardless of the error, if you’d like a sequence of instructions to be presented, you may utilize those statements within the final block, and it will do so.

Throw

The throw statement can be used to throw an exception. It will show you what kind of mistake you’re making.

Syntax:

 try {
 / /  exception handling starts with try
 block
 } catch(  ExceptionName ea1 ) {
     / / errors are handled within the
 catch block
 } catch( ExceptionName e2 ) {
     / / more catch block
 } catch( ExceptionName eN ) {
     / / more catch block to handle
 multiple exception raised
 } finally {
   / / last block of the exception
 handling
 }

10. Define the term “Destructor” in detail. Give an example to demonstrate your point.

Answer- A destructor is a member that performs the exact opposite of a constructor. Destructors, unlike constructors, primarily erase the object. Just like the constructor, the name of the destructor must match exactly with the class name. A destructor block always starts with the tilde (~) symbol.

Syntax:

 ~class_name()
 {
 / /code
 }

A destructor is automatically utilized:

  1. After the program’s execution is completed
  2. When a program’s scope expires, a local variable is created.
  3. Whenever the delete operator is used in your software.

11. Comment on the different types of Serializations.

If we wish to send an object over the network, we must first turn it into a stream of bytes. Serialization is the conversion of a complicated object into a stream of bytes for storage (in a database, file, cache, etc.) or transport. Its primary function is to save an object’s state.

The process of converting an object from a stream of bytes to its original form is known as de-serialization.

Different types of Serializations are:

Binary Serialisation

All public, private, and read-only members are serialized and converted into a stream of bytes throughout this procedure. This is utilized when we wish to convert all of our items at once.

SOAP Serialization

Only public members are transformed to SOAP format in this process. This is a term that is used in the context of online services.

XML Serialization

Only public members are transformed to XML in this procedure. This is a one-of-a-kind serialization. System.Xml and System.Xml.Serialization is required namespaces.

12. What will the following code snippet provide as a result:

      using System;
      public class Program
             {

public static void Main(string[] args)
int[]  arr = new int[2];
arr[1] = 10;
Object o = arr;
int[] arr1 = (int[])o;
arr1[1] = 100;
Console.WriteLine(arr[1]);
((int[])o)[1] = 1000;
Console.WriteLine(arr[1]);
}
}

a) 10

10

b) 10

100

c) 10

1000

d) 100

1000

Answer. OPTION: d)

   

About the Author

ByteScout Team ByteScout Team of Writers ByteScout has a team of professional writers proficient in different technical topics. We select the best writers to cover interesting and trending topics for our readers. We love developers and we hope our articles help you learn about programming and programmers.  
prev
next