Subscribe - It's FREE!!

Stay Connected Here

Stay Updated With Us Here



Google

Advanced Interview Questions and answers on C#.NET - Part 1


Share with WhatsApp


IMInfo.in -Advanced Interview Questions and answers on C#.NET - Part 1


On top of my previous posts Advanced .NET Interview Questions - Garbage Collection - Part 1 and Advanced .NET Interview Questions - Garbage Collection - Part 2, in this post we will discuss some advanced level interview questions on C#.NET, these interview questions I had faced in my last job hunt in PUNE for the post of Sr. Software Engineer.

Feel free to elaborate more on any of these questions, speacially on answers :-) in comment section which will help us to have more accurate answers.

One nonstatic class is having static and nonstatic method. then how to call nonstatic method in static method? and what if class is static?

-----call nonstatic method in static method if class is nonstatic---------

 public class myNonStaticClass
    {
        public static string myStaticMethod()
        {
           //to call non-static method from static method --- as like any other class nonstatic method
           myNonStaticClass onj = new myNonStaticClass();
           return    onj.myNonStaticMethod();
        }
        public string myNonStaticMethod()
        {
            return "hi from non static method";
        }
    }


-------Call static method in nonstatic method if class is nonstatic------------

 public class myNonStaticClass
    {
        public static string myStaticMethod()
        {
            return "hi from static method";
        }
        public string myNonStaticMethod()
        {
            return myStaticMethod();
        }
    }

---and if class is static, then we cannot declare instance i.e.non-static method in it.

error will come : cannot declare instance members in a static class.

If there are two interfaces having one method with same signature. and your class is implementing both. and you have implemented that method only once. So in this case will it work or any warning or error will come? if works then how compiler will come to know method of which interface you are implementing?

With this case no any error will come nor any warning because as per interface definition interface contains only the signatures of methods which we inplement in our inherited classes and with above case we are satisfying the definition.

And about how would compiler know, it depends on how we are implementing the interface. we can implement interface in two ways i.e. Implicit Implementation and Explicit Implementation (Read more about this)

What is partial class? and why Microsoft has introduced it?

We all know that Partial class allows us to split the definition of a class over two or more source files. Each source file contains a section of the type or method definition, and all parts are combined when the application is compiled. (Read More).

When it comes "Why Microsoft has introduced it?" we usually say "When working on large projects, spreading a class over separate files enables multiple programmers to work on it at the same time".

But when I had been asked this question, interviewer was expecting some different explanation i.e. Microsoft has introduced Partial Classes because it was their own requirement to produce the automatically generated source code.

Visual Studio uses this approach when it creates Windows Forms, Web service wrapper code, and so on. We can create code that uses these classes without having to modify the file created by Visual Studio.

So we can put some extra impression by adding above line in to our answer. :-)

Do you know Extension methods? Can we define extension methods for static class?

Extension methods are very well known by most of the developers, by definition, Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type.

Extension methods are a special kind of static method, but they are called as if they were instance methods on the extended type. For client code written in C# and Visual Basic, there is no apparent difference between calling an extension method and the methods that are actually defined in a type.

Read this MSDN Guide to know how to implement and call a Custom Extension Method.

Googly comes when interviewer asks , "Can we define extension method for a class which itself is a static class?"

To answer this, we cannot define extension methods for static class because Extension methods require an instance of an object and we cannot instantiate a static class.
It will throw following error :
 "static types cannot be used as parameters".

Can we declare structure as protected?

Many of us know more about classes as we are using it more, but when it comes to "Structure" we may get confused with some facts about it. So be yourself known about "Structure" too while preparing for interview.

To answer this question, We cannot declare structure as "protected",because "protected" is related to inheritance and A struct cannot inherit from another struct or class, and also it cannot be the base of a class.(All structs inherit directly from System.ValueType, which inherits from System.Object)


Hope you all have benefited from these quetions and answers, I will share more question in my next post of this series.

Till then, Read more and gain more! Happy job Hunting!!

 



If you enjoyed this post take 5 seconds to share it! Be Socialable. :-)

Share with WhatsApp

Posts To Read Next

Advanced .NET Interview Questions - Garbage Collection - Part 1

This post contains some advanced .NET Interview Questions on the topic Garbage Collection.


Advanced .NET Interview Questions - Garbage Collection - Part 2

This post contains some advanced .NET Interview Questions on the topic Garbage Collection.


How to Prepare for the interviews - Simple but important tips - Part 3 - Prepare for HR Round

In this third post of my interview tips series, I am going to share some tips about how to prepare for HR Round. Also about things to do and things to not to do during the same.


How to Prepare for the interviews - Simple but important tips - Part 1 - Resume Writing

In this post I will share some interview tips from my own personal experience which will help you to prepare for the interview. In part one I will cover how to design a resume which will put your first impression smartly.


How to Prepare for the interviews - Simple but important tips - Part 2 - Things that matter during interview

In this second post of my interview tips series, I am going to share some tips about how to behave during the interview. Also about how to prepare for the interview and maintain self confidence during the interview.


Your opinion is valuable for us! Comments, suggetions are welcome.


Submit your Email Id to stay updated with us and get notified with our new posts. It's FREE!
We know this popup is disturbing you!
But We would greatly appreciate if you share us with your friends below!

It will not take more than 2 seconds but will motivate us greatly to write more,share more!

x