Wednesday, August 26, 2020

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.

Why main method is static in C#?

The Main method states what the class does when executed and instantiates other objects and variables. The main method is static since it is available to run when the C# program starts. It is the entry point of the program and runs without even creating an instance of the class.

What is the static keyword in C#?

In C# terms, “static” means “relating to the type itself, rather than an instance of the type”. You access a static member using the type name instead of a reference or a value, e.g. Guid. NewGuid(). In addition to methods and variables, you can also declare a class to be static (since C# 2.0).

Can we declare constructor as private?

Yes, we can declare a constructor as private. 

If we declare a constructor as private we are not able to create an object of a class. We can use this private constructor in the Singleton Design Pattern.

Can abstract class have constructor?

Yes, Abstract Classes can have constructors!

An abstract class can have a constructor though it cannot be instantiated. But the constructor defined in an abstract class can be used for the instantiation of a concrete class of this abstract class.

Can you have a static method in a non static class?

You can define one or more static methods in a non-static class. Static methods can be called without creating an object. You cannot call static methods using an object of the non-static class.