Results 6 - 10 of 11

What is Static class and its difference with Non-Static class?


A Static class is a class that cannot be instantiated i.e. its object cannot be created.
Static class

      Has the keyword static.

      Cannot be instantiated and no object can be created.

      Keyword new is not supported.

      Objects and Functions can be accessed by class name.

      Mandatory for functions and methods to be marked static i.e. can contain only Static members.

      Members cannot be accessed using this keyword.

      Only one instance can be created.
Example:
public static class B
{
    public static object E;
 
    public static void F()
    {
    }
 
    public static void G()
    {
        //Valid
        var q = E;
 
        //Valid
        F();
    }
}
 
Non-Static class

      Does not have the keyword static.

      Can be instantiated and object can be created.

      Keyword new is supported.

      Objects and Functions cannot be accessed by class name unless marked static.

      Not mandatory for functions and methods to be marked static i.e. can contain Static as well as Non-Static members.

      Non-Static members can be accessed using this keyword. But Static members cannot be accessed using this keyword.

      Multiple instances can be created.
Example:
public class A
{
    public object C;
    public static object G;
 
    public static void D()
    {
        //Invalid
        var p = C;
 
        //Valid
        var q = G;
 
        //Invalid
        E();
    }
    public void E()
    {
        //Valid.
        var p = this.C;
 
        //Valid
        var q = G;
 
        //Valid
        D();
    }
}

What is Namespace in C#? Give example of in-built Namespace in .Net?


A Namespace is used to organize classes. Example of in-built .Net Namespace is the System Namespace.

      One can create his own Namespace and define the scope of the classes belonging to the Namespace.

      Keyword using is used in order to access a class belonging to a particular Namespace.

      A class belonging to a Namespace can be accessed only when the Namespace with the help of using directive.

      A namespace does not have any access modifiers i.e. namespace cannot be public, private, etc.

      A namespace can belong to multiple assemblies and also a single assembly can have multiple namespaces.
Example:
namespace MySpace
{
    public class A
    {
        public static void Fun()
        {
        }
    }
}
 
namespace YourSpace
{
    //Accesing the Namespace
    using MySpace;
    public class B
    {
        public void Fun()
        {
         //Accessing the class of external namespace
         A.Fun();
        }
    }
}

What is a Sealed class in C#?


A Sealed class is a class which cannot be inherited. The sealed keyword is used to prohibit inheritance of a particular class in C#.
A sealed class can be public as well as private.
Example:
public sealed class A
{
    public void Fun()
    {
    }
}
//Compiler Error: 'B': cannot derive from sealed type 'A'
public class B : A
{
    public static void Fun()
    {
    }
}
public class C
{
    public static void Fun()
    {
        //Valid
        A a = new A();
        a.Fun();
    }
}

What is an Internal class in C#?


An Internal class is a class which cannot be used outside its Assembly. The internal keyword is used to mark a particular class Internal i.e. it restrict its access outside the Assembly.

      An Assembly could be a Project, a DLL or an EXE.

      Inside the Assembly, the internal class is like public class.

      An internal class can be inherited within the Assembly.
Example:
internal class A
{
    public void Fun()
    {
    }
}
//Valid
public class B : A
{
    public static void Fun()
    {
    }
}
public class C
{
    public static void Fun()
    {
        //Valid
        A a = new A();
        a.Fun();
    }
}

What is an Abstract class in C#?


An Abstract class is a special class which is majorly used for inheritance and it cannot be instantiated.

      Cannot be instantiated i.e. object cannot be created using the new keyword.

      Can contain both Abstract and Non-Abstract members.

      Abstract members are simply declared and are defined in the classes deriving the Abstract class.

      Abstract members are defined by using the override keyword.

      Non-Abstract members are defined within the Abstract class.

      Non-Abstract members can be accessed within the derived classes only if marked public or protected.

      Private Non-Abstract members are not accessible outside the Abstract class.

      Abstract and Non-Abstract members can be accessed using the derived classes.

      Does not support Multiple Inheritance.
Example:
public abstract class A
{
    public abstract void Fun1();
    public void Fun4()
    {
    }
    protected void Fun5()
    {
    }
    private void Fun6()
    {
    }
}
public class B : A
{
    public override void Fun1()
    {
    }
    public void Fun2()
    {
        //Valid
        Fun4();
        Fun5();
 
        //Compiler Error: Cannot create an instance of the abstract class or interface
        Fun6();
    }
}
public class C
{
    public static void Fun3()
    {
        //Compiler Error: Cannot create an instance of the abstract class or interface 'A'
        A a = new A();
       
        //Valid
        B b = new B();
        b.Fun1();
        b.Fun2();
        b.Fun4();
    }
}

Results 6 - 10 of 11