Friday, April 28, 2023

Wipro Answer

 Question: What is the version of C# are you using?

Ans: C# 8.0 version currently  using.

 Visual Studio 2019

.NET Core - 3.0 

C# 8.0

Can I change the default C# version?

Yes, you may.

If you open your .csproj project file in Notepad or any text editor, you can add a new property group and set the LangVersion value to specify the language version your project will use. The default proj file looks like the following in XML.

<PropertyGroup> <LangVersion>8.0</LangVersion> </PropertyGroup>


Method Overriding Example;

If you have phone:

Then there must be ring facility in it:

Now your phone can be of any type, like it can be cellular, satellite or landline, these all types of phones will also have the same or a different functionality (based on their attribute).


Now whenever you receive a call, the caller doesn’t know whether you have a cellular phone, landline phone or anything else, he/she just calls you and according to that call operation your phone rings. In the case of method overriding, your phone works as a class and the ring is the functionality.





Method Overloading Example:

common example of method overloading is formatting a string using the Format method. The string can be formatted using input types such as int, double, numbers, chars, and other strings.


Question 3: Can inerface pass as parameter to method?

Ans: Yes.

Question 4: singleton class vs static class?

Real-World Examples of Singleton Class:

Some of the real-world scenarios where you can use the singleton design pattern are as follows

  • Managing Database Connections
  • Logging
  • Caching
  • Data Sharing
  • Application Configuration Management


Differences:

1. We can dispose the Singleton object but not static class .

2. We can clone the Singleton Class object (both Deep Copy and Shallow Copy of the Clone Object is possible using the MemberwiseClone method) whereas it is not possible to clone a static class.

3. Singleton means a single object across the application lifecycle, so the scope is at the application level. As we know the static class does not have any Object pointer, so the scope is at the App Domain level.


Question 5:what is sealed class why we use.

Ans: when we use shield can not inherit the class.


Question: 6 what is using block:

Question 7: how can we restrict object to be created only 3 instance?

Ans: 

public class ClassA

        {

            private static int _InstanceCount=0;

            public   ClassA()

            {

                if (_InstanceCount == 2)

                {

                    throw new Exception("We con not create more then 2 instance");

                }

                else

                {

                    _InstanceCount++;

                }

            }

-------------------------------------------------------------------------------------------------------------------

Question 8 :What is the difference between eager loading and lazy loading?

Lazy loading - is good when handling with pagination like on page load list of users appear which contains 10 users and as the user scrolls down the page an API call brings next 10 users. It's good when you don't want to load the entire data at once as it would take more time and would give a bad user experience.

Use Lazy Loading when you are using one-to-many collections.

Eager loading - is good as other people suggested when there are not many relations and fetch entire data at once in a single call to the database.


Eager loading use include method lo load the data at one hit.


Eager Loading helps you to load all your needed entities at once; i.e., all your child entities will be loaded at single database call. This can be achieved, using the Include method, which returs the related entities as a part of the query and a large amount of data is loaded at once.





Question 9:  Anonymous Method in c#

Ans: It is method without name & return type. It has only body and optional parameter and return type.

It created using delegate keyword.



1.Anonymous method can be defined using the delegate keyword

2.Anonymous method must be assigned to a delegate.

3.Anonymous method can access outer variables or functions.

4.Anonymous method can be passed as a parameter.

5.Anonymous method can be used as event handlers.


Anonymous Method Limitations:

  • It cannot contain jump statement like goto, break or continue.
  • It cannot access ref or out parameter of an outer method.
  • It cannot have or access unsafe code.
  • It cannot be used on the left side of the is operator.
Question 10: IEnumerable VS IQueryable
Ans:


  • IEnumerable exists in System.Collections Namespace.

  • IEnumerable can move forward only over a collection, it can’t move backward and between the items.

  • IEnumerable is best to query data from in-memory collections like List, Array, etc.

  • While query data from a database, IEnumerable execute a select query on the server side, load data in-memory on a client-side and then filter data.

  • IEnumerable is suitable for LINQ to Object and LINQ to XML queries.

  • IEnumerable supports deferred execution.

  • IEnumerable doesn’t support custom query.

  • IEnumerable doesn’t support lazy loading. Hence not suitable for paging like scenarios.

  • Extension methods support by IEnumerable takes functional objects.
MyDataContext dc = new MyDataContext ();
IEnumerable<Employee> list = dc.Employees.Where(p => p.Name.StartsWith("S"));
list = list.Take<Employee>(10); 



SELECT [t0].[EmpID], [t0].[EmpName], [t0].[Salary] FROM [Employee] AS [t0]
WHERE [t0].[EmpName] LIKE @p0




  • IQueryable exists in System. Linq Namespace.

  • IQueryable can move forward only over a collection, it can’t move backward and between the items.

  • IQueryable is best to query data from out-memory (like remote database, service) collections.

  • While query data from a database, IQueryable execute the select query on the server side with all filters.

  • IQueryable is suitable for LINQ to SQL queries.

  • IQueryable supports deferred execution.

  • IQueryable supports custom query using CreateQuery and Execute methods.

  • IQueryable support lazy loading. Hence it is suitable for paging like scenarios.

  • Extension methods support by IQueryable takes expression objects means expression tree.



MyDataContext dc = new MyDataContext ();
IQueryable<Employee> list = dc.Employees.Where(p => p.Name.StartsWith("S"));
list = list.Take<Employee>(10); 

SELECT TOP 10 [t0].[EmpID], [t0].[EmpName], [t0].[Salary] FROM [Employee] AS [t0]
WHERE [t0].[EmpName] LIKE @p0


Question:



Internal: nternal members not accessible outside the assembly either using object creating or in a derived class.

 Protected Internal:
 Protected Internal member works as Internal within the same assembly and works as Protected for outside the assembly.

Private Protected:
This compound access modifier is introduced to overcome the limitation of Protected Internal. In a private protected access modifier, the derived class from the different assembly cannot access the members which are Private Protected.

Question: 6

Ans: String:

String is immutable, Immutable means if you create string object then you cannot modify it and It always create new object of string type in memory.

Example:    

string strMyValue = "Hello Visitor"; // create a new string instance instead of changing the old one strMyValue += "How Are"; strMyValue += "You ??";


Stringbuilder:

StringBuilder is mutable, means if create string builder object then you can perform any operation like insert, replace or append without creating new instance for every time.it will update string at one place in memory doesnt create new space in memory.

 Example:

 StringBuilder sbMyValue = new StringBuilder("");

 sbMyValue.Append("Hello Visitor");

 sbMyValue.Append("How Are You ??");

 string strMyValue = sbMyValue.ToString();

 -----------------------------------------------------------------------------------------------------


Question 10: can we overwrite a constructor?

Ans:  No, you can't override constructors. The concept makes no sense in C#, because constructors simply aren't invoked polymorphically.

Question 11: can we create constructor in abstract class.

Ans: NO.


Question 12 : How Garbage collector work.

Ans:

GC.Collect():

The garbage collector’s Collect() method instructing the GC to clear off or deallocate all the inactive object’s memory. This is a very expensive operation and goes to all the generation (0,1,2) to clear off the memory or deallocate the objects in one shot.


GC.SupressFinalize()

to prevent the finalizer from releasing unmanaged resources that have already been freed by the IDisposable.Dispose implementation.


Question: 7 

Understanding Execution Order of Middleware in ASP.NET Core.

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.Use(async (context, next) =>
{
await context.Response.WriteAsync("Use Middleware1 Incoming Request \n");
await next();
await context.Response.WriteAsync("Use Middleware1 Outgoing Response \n");
});
app.Use(async (context, next) =>
{
await context.Response.WriteAsync("Use Middleware2 Incoming Request \n");
await next();
await context.Response.WriteAsync("Use Middleware2 Outgoing Response \n");
});
app.Run(async context => {
await context.Response.WriteAsync("Run Middleware3 Request Handled and Response Generated\n");
});

}

Output: