Interview questions :
C# is a modern, general-purpose programming language that can be used to perform a wide range of tasks. It consists of thousands of pre-built classes and interfaces that lets programmer to write powerful code in very less time.
Variables and data types:
C# provides two types of data types: Value types and Reference types.
A Value type data type stores copy of the value whereas the Reference type data types stores the address of the value. C sharp provides great range of predefined data types but it also gives the way to create user defined data types.
Encapsulation and abstraction :
Encapsulation and abstraction is the advanced mechanism in C# that lets your program to hide unwanted code within a capsule and shows only essential features of an object. Encapsulation is used to hide its members from outside class or interface, whereas abstraction is used to show only essential features.
In C# programming, Encapsulation uses five types of modifier to encapsulate data. These modifiers are public, private, internal, protected and protected internal. These all includes different types of characteristics and makes different types of boundary of code.
What is encapsulation?
Encapsulation is the process of hiding irrelevant data from the user. To understand encapsulation, consider an example of a mobile phone. Whenever you buy a mobile, you don’t see how circuit board works. You are also not interested to know how digital signal converts into the analog signal and vice versa. These are the irrelevant information for the mobile user, that’s why it is encapsulated inside a cabinet.
In C# programming, we will do the same thing. We will create a cabinet and keep all the irrelevant information in it that will be unavailable to the user.
What is abstraction?
Abstraction is just opposite of Encapsulation. Abstraction is a mechanism to show only relevant data to the user. Consider the same mobile example again. Whenever you buy a mobile phone, you see their different types of functionalities as a camera, mp3 player, calling function, recording function, multimedia etc. It is an abstraction because you are seeing only relevant information instead of their internal engineering.
What is Access Specifiers in C#?
Access Specifiers defines the scope of a class member. A class member can be variable or function.
Public Access Specifiers (C#): The class member, that is defined as a public can be accessed by other class members that are initialized outside the class. A public member can be accessed from anywhere even outside the namespace.
Private Access Specifiers (C#): The private access specifiers restrict the member variable or function to be called outside of the parent class. A private function or variable cannot be called outside of the same class. It hides its member variable and method from other class and methods. However, you can store or retrieve the value from private access modifiers using get the set property.
Protected Access Specifiers C#: The protected access specifier hides its member variables and functions from other classes and objects. This type of variable or function can only be accessed in child class. It becomes very important while implementing inheritance.
C# Internal Access Specifiers: The internal access specifier hides its member variables and methods from other classes and objects, that is resides in other namespace. The variable or classes that are declared with internal can be access by any member within application. It is the default access specifiers for a class in C# programming.
C# Protected Internal Access Specifiers: The protected internal access specifier allows its members to be accessed in derived class, containing class or classes within same application. However, this access specifier rarely used in C# programming but it becomes important while implementing inheritance.
C# Get Set Modifier: The get set accessor or modifier mostly used for storing and retrieving the value from the private field. The get accessor must return a value of property type where set accessor returns void. The set accessor uses an implicit parameter called value. In simple word, the get method used for retrieving the value from private field whereas set method used for storing the value in private variables.
Classes
A class in C# is a blueprint or template that is used for declaring an object. However, there is no need to declare an object of the static class. A class consists of member variables, functions, properties etc. A method is a block of code in C# programming. The function makes program modular and easy to understand.
The class is a template, declaration or blueprint that is used for classifying the object. It encapsulates variable members, functions, structure, properties and many more components. It is the basic building block of object-oriented programming. In order to create a class, the class keyword is used.
C# Method Declaration
Method is the building block of object-oriented programming. It combines related code together and makes program easier. In C# method declaration, you can declare method by following way:
<Access Specifier> <Return Type> <Method Name>(Parameter list)
{
Body // void is a return data type that return nothing
}
C# Static Method And Variables:
Whenever you write a function or declare a variable, it doesn’t create an instance in a memory until you create an object of the class. But if you declare any function or variable with a static modifier, it directly creates an instance in a memory and acts globally. The static modifier doesn't reference any object.
C# Main Method:
In C# programming the Main method is where program starts execution. It is the main entry point of program that executes all the objects and invokes method to execute. There can be only one Main method in C#. However, the C# Main method can be void or int return type. It must be inside a class or struct and must be declared with static modifier. It is the main place where a program starts the execution and end. The Main method can have a parameter and these parameters are known as zero-indexed command line argument. In lateral chapter you will learn more about command line argument.
Parameters:
The parameter is used to passing information to a function. Based on the type of parameters, function responses, and returns value. A parameter can be passed to a function by two ways: Passing parameter by value and passing a parameter by reference.
As mentioned in the previous chapter, there are two types of the parameter that can be passed in function. One is Value type parameter and another is reference type parameter. Before understanding both types of parameters in C#, you will have to understand Value type and Reference type.
There are two ways to allocating space in memory. One is a value type and another is a Reference type. When you create int, char or float type variable, it creates value type memory allocation whereas when you create an object of a class, it creates reference type memory allocation.
Value Type: A value type variable directly contains data in the memory.
Reference Type: A Reference type variable contains memory address of value.
Constructors: Constructors are the special method of the class which is used when initializes the object. It looks like similar to method but it has the same name as a class. It is created with the same name of the class and notable thing is that it never returns value so you can’t use this method for returning some value.
WHAT IS FILE HANDLING IN C#?
To save the information permanently on the disk or reading information from the saved file through C# is known as File Handling. C# File Handling uses a stream to save or retrieve information.
FILES AND STREAM
A file is a collection of data stored on a disk with specific name, extension and directory path. When you open File using C# for reading and writing purpose it becomes Stream. A stream is a sequence of bytes traveling from a source to a destination over a communication path.
WHY I NEED TO LEARN FILE HANDLING?
As a C# programmer, several times you need to save information on a disk. You will not get database everywhere to save information and your project may require saving information in a txt file, doc file, xls file, pdf files or any other file types. So, you must know the concept of saving data in a file.
System.IO Namespace which is a collection of classes that is responsible for reading, writing and accessing of files.
System.IO Namespace is a collection of classes, methods, enumeration and types that is responsible for reading and writing files and data streams. All the File Handling operations require System.IO namespace.
WHAT IS FILESTREAM CLASS IN C#?
FileStream Class is used to perform the basic operation of reading and writing operating system files. FileStream class helps in reading from, writing and closing files.
In order to use FileStream class you need to include System.IO namespace and then create FileStream Object to create a new file or open an existing file.
FileStream <object_name> = new FileStream( <file_name>, <FileMode Enumerator>, <FileAccess Enumerator>, <FileShare Enumerator>);
FileMode – It specifies how to operation system should open the file. It has following members
Append - Open the file if exist or create a new file. If file exists then place cursor at the end of the file.
Create - It specifies operating system to create a new file. If file already exists then previous file will be overwritten.
CreateNew - It create a new file and If file already exists then throw IOException.
Open – Open existing file.
Open or Create – Open existing file and if file not found then create new file.
Truncate – Open an existing file and cut all the stored data. So the file size becomes 0.
C# Inheritance With Programming Example
Inheritance is the one of the important pillar of Object Oriented Programming (OOP).
Inheritance allows you to access members of base class in child class. It enables you to create a child class that can access and use all the functionality of its base class. This way you can keep common variable and functions in a base class and use them as many times as you want in child class. Code re-usability makes your program simpler and efficient.
What is Polymorphism?
Polymorphism is one of the fundamental concept and strongest pillars of Object Oriented Programming. Simply Polymorphism means – One Interface, Multiple Functions. It is a Greek word that means Many Forms.
Static Polymorphism
In static Polymorphism, you bind a function with an object during compile time. In simple word, you tag a function with an object while writing program. It is also called early binding. In static polymorphism, Function Overloading gets implemented.
Dynamic Polymorphism
It is also called as Late Binding or Method Overriding or Dynamic Polymorphism. In dynamic polymorphism, we override base class function using virtual & override keywords.
Function Overloading
In function overloading, a function works differently based on parameters. A single function can have different nature based on a number of parameters and types of parameters. For example, you have a function Sum() that accepts values as a parameter and print their addition. You can write multiple functions with name Sum() but the parameter signature must be different.
What is Runtime Polymorphism?
Runtime Polymorphism is also known as Dynamic Polymorphism, Late Binding, Method overriding etc. Whereas in static polymorphism we overload a function; in dynamic polymorphism we override a base class function using virtual or override keyword. Method overriding means having two or more methods with the same name, same signature but with different implementation.
C# - What is OOP?
OOP stands for Object-Oriented Programming
Object-oriented programming has several advantages over procedural programming:
OOP is faster and easier to execute
OOP provides a clear structure for the programs
OOP helps to keep the C# code DRY "Don't Repeat Yourself", and makes the code easier to maintain, modify and debug
OOP makes it possible to create full reusable applications with less code and shorter development time
C# - What are Classes and Objects?
Classes and objects are the two main aspects of object-oriented programming.
ex: class: Fruit, object: Apple, mango, banana
So, a class is a template for objects, and an object is an instance of a class.
When the individual objects are created, they inherit all the variables and methods from the class.