This is the time when we should start knowing about C# programming
keywords, main purpose of this post is to put all C# keywords into one place so
that we should be very familiar before starting C# programming
Now, we will start with Class and Object concept, whenever anyone will start to ask you something about C# OOP's concept, very first time question is "do you really know what is difference between "Class" and "Object" .
Difference Between Class and Object
1. Class is a collection of object while object is a instance of the class
2. Class is a concrete representation of an entity while Object is a physical or real entity
3. Class does not allocate memory while Object does allocate memory using "New" keyword
My First Program in C#
Using System;
namespace FirstProgram
{
Public class HelloWorld
{
Public Static Void Main()
{
Console.WriteLine("My First Program in C#");
}
}
}
What do you mean by public static void Main() ?
Public is a access modifier which mean that method can be accessible anywhere in application
Static means method will be accessible through Class Name
Void is a return type
Main will define the entry point of an application
What are valid signatures for the Main function?
Ø public static void Main()
Ø public static int Main()
Ø public static void Main( string[] args )
Ø public static int Main(string[] args )
What is string[] args in Main method? What is there use?
Ans: The string[] args may contain any number of Command line arguments and can pass to the Main() method.
What is the return value of main method by default?
Ans: Void
Tricky Question
Can we have more than one Main method in C# program ?
Not possible in the same class because Main method is the entry point of a C# program, when the application is started, then Main method is the first method that is being invoked.
Note: If you have more than one class that has a Main method, you must compile your program with the /main compiler option to specify which Main method to use as the entry point in the command prompt.
Through command line : CSC /main:classname filename.cs
Does Main() will be executed without Static modifier in C# program?
No, because the main method is the first access point for any program and has to be called automatically.. Since it is static it gets loaded automatically even before the object of that class is being created and Main() does not require any object to be called!!!!
Can we set the specifier of the main() method in C# as private?
See code below:
private static void Main()
{
//This code isn't invoked automatically by other assemblies
}
Different Access Modifier in C#
1. Public : Can be access anywhere in the program
2. Private : Access limited to containing class
3. Protected : Can access to the class and it's derive class
4. Internal : Access limited within assembly
5. Protected Internal : Can be protected or internal
6. Default : Default visibilitClass will be internal and Object will be private
Type of Classes
1. Base / Parent Class : Class will be inherited by derive class
2. Derived / Child Class : Derive class will inherit the functionality of base class and extend it
3. Abstract Class : Can't instantiate but can be inherited
4. Sealed Class : Can instantiate but can not be inherited
5. Static Class : Can't instantiate and can't inherited
6. Generic Class : Knowing the type of the class during run time
Tips
1. Sealed keywords can’t use with abstract keywords
Difference b/w Abstract class and interface
Example
using System;
namespace Interfaces
{
interface Car
{
void Drive();
}
interface Bus
{
void Drive();
}
class Demo : Car,Bus
{
void Car.Drive()
{
Console.WriteLine("Drive Car");
}
void Bus.Drive()
{
Console.WriteLine("Drive Bus");
}
public static void Main()
{
Demo DemoObject = new Demo();
((Car)DemoObject).Drive();
((Bus)DemoObject).Drive();
}
}
}
Tricky Question
Can Derive class have greater accessibility than their Base Class ? No
Correct order of declaring the class member in c# ? public , private , Protected
Are private class-level variables inherited? Yes, but they are not accessible
Is the following code legal?
using System;
private class Test
{
public static void Main(){ }
}
No, a compile time error will be generated stating "Namespace elements cannot be explicitly declared as private, protected, or protected internal"
Difference between Class and Struct
1. Classes are reference type and can be stored on heap while struct are value type and can store in stack
3. Dictionary object is create using key value pairs while for hashtable object not required
4. Hashtable will search in faster way because uses hash key to search an element
Different type of comment has supported by C#?
Now, we will start with Class and Object concept, whenever anyone will start to ask you something about C# OOP's concept, very first time question is "do you really know what is difference between "Class" and "Object" .
Difference Between Class and Object
1. Class is a collection of object while object is a instance of the class
2. Class is a concrete representation of an entity while Object is a physical or real entity
3. Class does not allocate memory while Object does allocate memory using "New" keyword
My First Program in C#
Using System;
namespace FirstProgram
{
Public class HelloWorld
{
Public Static Void Main()
{
Console.WriteLine("My First Program in C#");
}
}
}
What do you mean by public static void Main() ?
Public is a access modifier which mean that method can be accessible anywhere in application
Static means method will be accessible through Class Name
Void is a return type
Main will define the entry point of an application
What are valid signatures for the Main function?
Ø public static void Main()
Ø public static int Main()
Ø public static void Main( string[] args )
Ø public static int Main(string[] args )
What is string[] args in Main method? What is there use?
Ans: The string[] args may contain any number of Command line arguments and can pass to the Main() method.
What is the return value of main method by default?
Ans: Void
Tricky Question
Can we have more than one Main method in C# program ?
Not possible in the same class because Main method is the entry point of a C# program, when the application is started, then Main method is the first method that is being invoked.
Note: If you have more than one class that has a Main method, you must compile your program with the /main compiler option to specify which Main method to use as the entry point in the command prompt.
Through command line : CSC /main:classname filename.cs
Does Main() will be executed without Static modifier in C# program?
No, because the main method is the first access point for any program and has to be called automatically.. Since it is static it gets loaded automatically even before the object of that class is being created and Main() does not require any object to be called!!!!
Can we set the specifier of the main() method in C# as private?
Yes, by default Main
method is static. When the access specifier is set as private for the
Main() method, then other assemblies may not invoke this class' Main() method
as the starting point of the application. The startup scope gets limited to the
class in context itself.
See code below:
private static void Main()
{
//This code isn't invoked automatically by other assemblies
}
Different Access Modifier in C#
1. Public : Can be access anywhere in the program
2. Private : Access limited to containing class
3. Protected : Can access to the class and it's derive class
4. Internal : Access limited within assembly
5. Protected Internal : Can be protected or internal
6. Default : Default visibilitClass will be internal and Object will be private
Type of Classes
1. Base / Parent Class : Class will be inherited by derive class
2. Derived / Child Class : Derive class will inherit the functionality of base class and extend it
3. Abstract Class : Can't instantiate but can be inherited
4. Sealed Class : Can instantiate but can not be inherited
5. Static Class : Can't instantiate and can't inherited
6. Generic Class : Knowing the type of the class during run time
Tips
1. Sealed keywords can’t use with abstract keywords
2. Partial can’t use with enum and delegate
3. Static can’t
use with virtual, override and abstarct
Difference b/w Abstract class and interface
1. Abstract class have abstract and
non-abstract methods while in interface, by default all method are abstract
2. Abstract methods uses access modifier
while interface by default all method are public
3. Abstract class came from inheritance
family while interface not.
4. Abstract class can’t support
multiple inheritance but through interface can achieve
Note: If two interfaces
have same method name and both interface are inherited by a class then can be
accessible through explicit interface.
Example
using System;
namespace Interfaces
{
interface Car
{
void Drive();
}
interface Bus
{
void Drive();
}
class Demo : Car,Bus
{
void Car.Drive()
{
Console.WriteLine("Drive Car");
}
void Bus.Drive()
{
Console.WriteLine("Drive Bus");
}
public static void Main()
{
Demo DemoObject = new Demo();
((Car)DemoObject).Drive();
((Bus)DemoObject).Drive();
}
}
}
Tricky Question
Can Derive class have greater accessibility than their Base Class ? No
Correct order of declaring the class member in c# ? public , private , Protected
Are private class-level variables inherited? Yes, but they are not accessible
Is the following code legal?
using System;
private class Test
{
public static void Main(){ }
}
No, a compile time error will be generated stating "Namespace elements cannot be explicitly declared as private, protected, or protected internal"
Difference between Class and Struct
1. Classes are reference type and can be stored on heap while struct are value type and can store in stack
2. Classes have default constructor while struct not.
3. Classes can have
attribute and data member declaration while struct can have only
attributes/fields
4. Class supports
inheritance while struct not
5. Class used in large data
structure while struct uses in small data structure
Note: In Structure,
We can only initialize the indexer and static variable
Difference
between the instance constructor and static constructor
1.
Instance constructor initializes when instance of the object has created and
static constructor initializes before the instance of object is created.
2.
Instance constructor can access using instance of class while static
constructor can access using Classname
3.
instance constructor can have parameter while static constructor not
Difference between Value type and reference type
1. Value type stored
in the stack while reference type stored in the Heap
2. If copying a value from one to
another then both value are independent due to this if one value has modified
then it does not affect the other value while if copying the reference
from one to another then change in the one object will reflect in other because
both were pointing to the same location
Difference between Boxing and Unboxing
Boxing means convert a value type into reference
type with implicit conversion while Unboxing is the reverse way and can achieve
only through explicit conversion
Difference between Dictionary and Hashtable
1. Dictionary is displayed a data in the order we have added
while hashtable does not follow the order
2. If any data is searched in the Dictionary and not exist then will give
exception: KeyNotFoundException while hashtable will return null value3. Dictionary object is create using key value pairs while for hashtable object not required
4. Hashtable will search in faster way because uses hash key to search an element
Note: Hashtable
inherit Idictionary interface internally
What is the difference between the Method
Overloading and Method Overriding
1. Method overloading is a compile time polymorphism while Method overriding is
a run time polymorphism
2. In method overloading,
Method name will be same but may be have different number of parameter,
different order of parameter or difference type of parameter while in method
overloading change the method behaviour using Virtual/ Abstract in the base
class and Override/New in the derived class
Note: Static
keyword can’t use with Abstract / Virtual / Override keywords
Difference
between constant and read-Only
1. Constant is evaluated during compile time while read-Only is evaluated
during runtime
2. Constant has to declared and
initialized once while read-Only can declare and initialize or initialize in
the constructor region
3. Constant can’t be static while read-Only
can be static
Difference
between Array, List and Array List
1. Array can have multiple
value of the same type and size is fixed
2. List can have multiple value
of the same type and size is not fixed, it’s dynamic
3. Array List can have multiple value of
the multiple type and size is not fixed, it’s dynamic
Difference
between string and string builder
1. String is immutable while string builder is mutable
2. String a derive from system.string while string builder is derived from system.text
Note: String will always create a new instance whenever any modification happened
1. String is immutable while string builder is mutable
2. String a derive from system.string while string builder is derived from system.text
Note: String will always create a new instance whenever any modification happened
Different
type of array and defined from?
Linear
array, multi-dimensional array and jagged array and derived from system.array
Different type of comment has supported by C#?
/
/
- Single line,
/*
*/
- Multiple and
///
- XML comment
What
is difference between Delegate and Event?
1. Delegate is a type safe function pointer which is used to encapsulate a method and pass as a parameter while event add extra layer on delegate thus forming a publisher and subscriber model and on the bottom of layers used delegate
2. In Event, listeners (publisher) who are interested in receiving some events they subscribe to the source (subscriber).
1. Delegate is a type safe function pointer which is used to encapsulate a method and pass as a parameter while event add extra layer on delegate thus forming a publisher and subscriber model and on the bottom of layers used delegate
2. In Event, listeners (publisher) who are interested in receiving some events they subscribe to the source (subscriber).
Note: Events are directly invoked in the derived class if
the events are declared as public in the base class.
Covariance and Contra-variance
Covariance permits a method to have a more derived
return type than what is defined in the delegate. While Contra-variance permits
a method with parameter types that are less derived than in the delegate type.
Note: Covariance and contra-variance provide a degree of
flexibility when you match method signatures with delegate
types.
Nice start keep on updating Gupta Ji!
ReplyDelete