Friday, August 28, 2020

Generics example

namespace Generics

{

    public class MainClass

    {

        private static void Main()

        {

            //bool IsEqual = ClsCalculator.AreEqual<int>(10, 20);

            //bool IsEqual = ClsCalculator.AreEqual<string>("ABC", "ABC");

            bool IsEqual = ClsCalculator.AreEqual<double>(10.5, 20.5);

            if (IsEqual)

            {

                Console.WriteLine("Both are Equal");

            }

            else

            {

                Console.WriteLine("Both are Not Equal");

            }

            Console.ReadKey();

        }

    }

    public class ClsCalculator

    {

        public static bool AreEqual<T>(T value1, T value2)

        {

            return value1.Equals(value2);

        }

    }

}

Thursday, August 27, 2020

What is IEnumerable in C#?

 IEnumerable in C# is an interface that defines one method, GetEnumerator which returns an IEnumerator interface. This allows read-only access to a collection then a collection that implements IEnumerable can be used with a for-each statement.

Wednesday, August 26, 2020

Is Vs as C#?

 The difference between is and as operators are as follows: The is operator is used to check if the run-time type of an object is compatible with the given type or not whereas as operator is used to perform conversion between compatible reference types or Nullable types.

What is difference between generic and non generic in C#?

A Generic collection is a class that provides type safety without having to derive from a base collection type and implement type-specific members. The key difference between Generic and Non-generic Collection in C# is that a Generic Collection is strongly typed while a Non-Generic Collection is not strongly typed.

What is generic list in C#?

In c#, List is a generic type of collection so it will allow storing only strongly typed objects i.e. elements of the same data type, and the size of the list will vary dynamically based on our application requirements like adding or removing elements from the list.

Can we store different types in an array in C#?

In c# we use an object[] array to store different types of data in each element location. You can use an object[] (an object array), but it would be more flexible to use List<object>. It satisfies your requirement that any kind of object can be added to it, and like an array, it can be accessed through a numeric index.

List vs array in C#

Arrays are strongly typed which means it can store only specific type of items or elements. Arraylist are not strongly typed. Array cannot accept null. ArrayList can accepts null.

What are circular references in C#?

 A circular reference occurs when two or more interdependent resources cause lock condition. This makes the resource unusable.

To handle the problem of circular references in C#, you should use garbage collection. It detects and collects circular references. The garbage collector begins with local and static and it marks each object that can be reached through their children.

Through this, you can handle the issues with circular references.

Let’s say the following classes are in a circular reference. Here both of them depends on each other 

public class Hyd {

   Wgl Two;

}

public class Wgl {

   Hyd one;

}

to solve this issue using interfaces

public interface ourInterface {

}

public class Hyd{

   ourInterface Two;

}

public class Wgl: myInterface {

   Hyd one;

}


Monday, August 24, 2020

What is IDisposable in C#?

IDisposable is an interface that contains a single method, Dispose of (), for releasing unmanaged resources, like files, streams, database connections, and so on.

Can we have 2 main methods in C#?

In a C# application, we can have multiple Main methods but only one of them with valid Main signature can be treated as Startup or Entry Point of the application.