Q1. What is namespace in C#?
Q2. What is extension method in C#?
Q3. In multiple layer architecture if you are asked to implement exception handling in one layer, Which layer would you prefer?
Q4. Can I extend Integer class or String class using Extension method concept in C#?
Q5. What are the advantages of Interfaces?
--------------------------------------------------------------------------------------------------------------------------
Q1. What is namespace in C#?
Answer:
Q2. What is extension method in C#?
Q3. In multiple layer architecture if you are asked to implement exception handling in one layer, Which layer would you prefer?
Q4. Can I extend Integer class or String class using Extension method concept in C#?
Q5. What are the advantages of Interfaces?
--------------------------------------------------------------------------------------------------------------------------
Q1. What is namespace in C#?
Answer:
Namespace is way of organizing collection of relative classes and methods together.
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/namespaces/
--------------------------------------------------------------------------------------------------------------------------
Q2. What is extension method in C#?
Answer:
--------------------------------------------------------------------------------------------------------------------------
Q4. Can I extend Integer class or String class using Extension method concept in C#?
Answer:
Yes.
--------------------------------------------------------------------------------------------------------------------------
Q5. What are the advantages of Interfaces?
Answer:
--------------------------------------------------------------------------------------------------------------------------
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/namespaces/
--------------------------------------------------------------------------------------------------------------------------
Q2. What is extension method in C#?
Answer:
- Extension methods enable you to add methods to existing Class without making actual changes in original class.
- An extension method is a static method of a static class, where the "this" modifier is applied to the first parameter. The type of the first parameter will be the type that is extended.
- Using Extension method you can extend C# in-built types like string also. For example you want to extend the wordcount functionality in a String.
- Extension method can be used with custom classes for example if class1 has three methods already present m1(), m2(), m3(). Using extension method we can add two more methods m4(), m5() to same class Class1 without making changes in the class itself.
// Example of the extension methods
using System;
namespace ExtensionMethod {
// Here Geek class contains three methods
// Now we want to add two more new methods in it
// Without re-compiling this class
class Geek {
// Method 1
public void M1()
{
Console.WriteLine("Method Name: M1");
}
// Method 2
public void M2()
{
Console.WriteLine("Method Name: M2");
}
// Method 3
public void M3()
{
Console.WriteLine("Method Name: M3");
}
} }
// C# program to illustrate the concept of the extension methods
using System;
namespace ExtensionMethod {
// This class contains M4 and M5 method
// Which we want to add in Geek class.
// NewMethodClass is a static class
static class NewMethodClass {
// Method 4
public static void M4(this Geek g)
{
Console.WriteLine("Method Name: M4");
}
// Method 5
public static void M5(this Geek g, string str)
{
Console.WriteLine(str);
}
}
// Now we create a new class in which
// Geek class access all the five methods
public class GFG {
// Main Method
public static void Main(string[] args)
{
Geek g = new Geek();
g.M1();
g.M2();
g.M3();
g.M4();
g.M5("Method Name: M5");
}
}
}
using System;
namespace ExtensionMethod {
// Here Geek class contains three methods
// Now we want to add two more new methods in it
// Without re-compiling this class
class Geek {
// Method 1
public void M1()
{
Console.WriteLine("Method Name: M1");
}
// Method 2
public void M2()
{
Console.WriteLine("Method Name: M2");
}
// Method 3
public void M3()
{
Console.WriteLine("Method Name: M3");
}
} }
using System;
namespace ExtensionMethod {
// This class contains M4 and M5 method
// Which we want to add in Geek class.
// NewMethodClass is a static class
static class NewMethodClass {
// Method 4
public static void M4(this Geek g)
{
Console.WriteLine("Method Name: M4");
}
// Method 5
public static void M5(this Geek g, string str)
{
Console.WriteLine(str);
}
}
// Now we create a new class in which
// Geek class access all the five methods
public class GFG {
// Main Method
public static void Main(string[] args)
{
Geek g = new Geek();
g.M1();
g.M2();
g.M3();
g.M4();
g.M5("Method Name: M5");
}
}
}
--------------------------------------------------------------------------------------------------------------------------
Q3. In multiple layer architecture if you are asked to implement exception handling in one layer, Which layer would you prefer?
Answer:
If I have to choose one layer I will probably go with the top layer. Like for WebAPI application probably at controller level and in case of MVC application probably at mvc controller's level. Bubbling up the exception wont put much impact as such. We can always trace down where the exception has occurred. This way we can avoid logging at all levels. Only bubble up of the exception is possible not vise versa.
--------------------------------------------------------------------------------------------------------------------------If I have to choose one layer I will probably go with the top layer. Like for WebAPI application probably at controller level and in case of MVC application probably at mvc controller's level. Bubbling up the exception wont put much impact as such. We can always trace down where the exception has occurred. This way we can avoid logging at all levels. Only bubble up of the exception is possible not vise versa.
Q4. Can I extend Integer class or String class using Extension method concept in C#?
Answer:
Yes.
--------------------------------------------------------------------------------------------------------------------------
Q5. What are the advantages of Interfaces?
Answer:
- Multiple Inheritance.
- Enforce Implementation: Force to implement important methods which newbie developer might forget.
- Loose coupling: Now you don't depend on concrete class for creating objects and your work can proceed even when referred class in not yet complete.
- Dependency injection
No comments:
Post a Comment