C# Flashcards

1
Q

WHAT ARE THE PRINCIPLES OF OOPS?

A

The principles of Object-Oriented Programming (OOP) are a set of guidelines that define the way in which objects interact with each other. These principles provide a foundation for the design and development of software systems that are modular, flexible, and maintainable. The four primary principles of OOP are:
Why OOPs?
drawbacks of procedure oriented and structure oriented programming language.
What is OOPs?
Pillars of OOPs.
1. Encapsulation:
Wrapping up of the data and data hiding. Eg: properties, class, methods, access specifiers/modifiers.
The concept of encapsulation involves hiding the internal details of an object from the outside world and providing a well-defined interface for interacting with it. This protects the object from unauthorized access or modification and allows for better control over how the object is used.

  1. Inheritance: Inheritance is the mechanism by which one class can inherit the properties and behavior of another class. This allows for the reuse of code and the creation of new classes that extend or modify the behavior of existing ones.
    Why inheritance? reusability
  2. Polymorphism: Polymorphism refers to the ability of objects to take on multiple forms or behaviors based on the context in which they are used. This allows for more flexible and dynamic code that can adapt to changing requirements or environments.
  3. Abstraction: Abstraction involves focusing on the essential properties and behavior of an object and ignoring the non-essential details. This simplifies the complexity of a system and allows for better organization and understanding of the code.
    -C. Venkata Teja
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

WHAT IS INHERITANCE? WHY INHERITANCE IS IMPORTANT?

A

It is one of the Pillar in oops. Deriving a child class from Parent class. This child class automatically Inherit the properties of Parent class.
- It is very helpful for the code Reusability.
Type of Inheritance:
1.Single inheritance
2.Multiple inheritance
3.Multilevel inheritance
4.Hybrid inheritance

                                         ~ Mahathi Somisetty
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

WHAT ARE THE DIFFERENT TYPES OF INHERITANCE?

A

Inheritance is a fundamental concept in object-oriented programming (OOP) that allows classes to inherit properties and behavior from other classes. There are different types of inheritance in OOP, including:

Single inheritance: A class can inherit properties and behavior from only one parent class.

Multiple inheritance: A class can inherit properties and behavior from more than one parent class.

Multilevel inheritance: A class can inherit properties and behavior from a parent class, which in turn inherits from another parent class. For example, if class C inherits from class B, which inherits from class A, then C has access to properties and behavior defined in both B and A.

Hierarchical inheritance: A class can have more than one child class, each of which inherits properties and behavior from the same parent class. For example, if class A is the parent class and classes B and C are child classes, then both B and C inherit properties and behavior from A.

Hybrid inheritance: Hybrid inheritance is a combination of two or more types of inheritance, such as multiple inheritance and hierarchical inheritance. It is also sometimes called “virtual inheritance.”

By: Siva Billudu

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

HOW TO PREVENT A CLASS FROM BEING INHERITED in c#?

A

In C# you can use the sealed keyword in order to prevent a class from being inherited.

By sealing or NotInheritable a class in .Net environment, you ensure that no class can be derived from that class. In some programming scenarios (mostly used for security reasons ) demand that you should declare your class in such a way that it should not be derived.

Typically, it occurs if the functionality that is implemented by the class should not be changed, or more appropriately, overridden. The sole purpose of this class to provide some implementation that can be used within that class only. Moreover a sealed class is the last class in the hierarchy.

  • Abdul Aleem
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Why we need Abstraction?

A

Abstraction is a crucial concept in computer science, software engineering, and many other fields. It is the process of reducing complexity by hiding unnecessary details while highlighting the essential features of a system. Here are some reasons why we need abstraction:

Simplifies Complexity: Abstraction helps us to simplify complex systems by breaking them down into smaller, more manageable parts. By abstracting away the details that are not necessary for understanding the system’s behavior, we can focus on the essential features and relationships that matter.

Encapsulates Complexity: Abstraction also allows us to encapsulate complexity by hiding implementation details behind a simple interface. This makes it easier to modify and maintain the system without affecting its external behavior.

Increases Reusability: Abstraction promotes reusability by making it easier to extract and reuse common functionality across different systems. By abstracting away implementation details, we can create more generic and flexible components that can be used in a wide range of contexts.
- Rizwan RA

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Can you differentiate between Encapsulation and Abstraction?

A

Encapsulation:
Encapsulation is also a feature of OOPs. It is a Wrapping up of data and hides the code and data into a single entity .
- Encapsulation solves issues at implementation level
- It focuses on internal working.
- It process to contain information.
Analogy:
A capsule binds all its medicinal materials within it.
Example: Properties, access modifiers.

Abstraction:
Abstraction: Abstraction involves focusing on the essential properties and behavior of an object
- Encapsulation solves issues at design level
- It focuses on external outlook.
Example:
- abstract classes and interfaces in C#
- through designs you gain information.

encapsulation and abstraction are both important concepts in programming that help to improve code quality and make it easier to maintain and modify.
-Sohail

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

WHAT ARE ACCESS SPECIFIERS? WHAT IS THE DEFAULT ACCESS MODIFIER IN A CLASS?

A

ACCESS SPECIFIERS:Used to Control the Accessbility of class members.

Types of ACCESS SPECIFIERS:
1.Internal
2.public
3.private
4.protected
5.private protected
6.protected internal

The default access modifier in a class is internal.

By :Siva Billudu

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is Polymorphism?

A

Why polymorphism?

What - Polymorphism is a programming concept that refers to the ability of objects of different types to be treated as if they have the same interface or behavior. In other words, polymorphism allows objects of different classes to be used interchangeably if they have a common set of methods or properties.
How to implement??

Assigned to Rizwan

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

WHAT IS METHOD OVERLOADING? Why it is called compile time polymorphism?

A

Developing Multiple methods having same method name but having different parameter list is called Method overloading.

Example:
Demo(int a){
}
Demo(String s, int b){
}

Since this binding process is executed during compile time, that’s why it is known as Compile-Time Polymorphism.

by Ajay

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

WHAT IS THE DIFFERENCE BETWEEN OVERLOADING AND OVERRIDING?

A
  • Method overloading is a type of polymorphism, in which we can create multiple
    methods of the same name in the same class, and all methods work in different
    ways.
  • Method overriding is having methods with the same name and signature
    but in different classes.
  • Method Overloading will not use any special keywords.
    Method Overriding uses the VIRTUAL keyword for the base class method and the OVERRIDE
    keyword for the derived class method.
  • Method overloading doesn’t need inheritance. It is in the same class.
  • Method overriding needs inheritance (Base class/ Derived class). It is not
    possible in the same class

_ Aman Shankar

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

WHAT IS THE DIFFERENCE BETWEEN METHOD OVERRIDING AND METHOD HIDING?

A

1.Creating a method in the derived class with the same signature as a method in the base class is called as method overriding.
2.It is calledd runtime polymorphism.
3.Method overriding needs inheritance.
4.Method overriding needs hierachy level of the classes i.e. one parent class and other child class.
5.It must have same method name as well as the signatures or the parameters.
6.using the keyword virtual and override.
7.Method Overriding only redefines the implementation of the method.

1.Method hiding is also known as shadowing.
2.The method of the parent class is available to the child class without using the override keyword in shadowing.
3. The child class has its own version of the same function.
4.use the new keyword to perform shadowing.
5.Method Hiding can completely redefine the method.
V.Mounika

https://www.geeksforgeeks.org/difference-between-method-overriding-and-method-hiding-in-c-sharp/

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How will you differentiate abstract class and interface?
(Must not skip the digging down to this question)

A

Interfaces are used when you want to define a contract that multiple classes can implement. An interface defines a set of methods, properties, and events that a class must implement to satisfy the contract.
Abstract classes, are used when you want to provide a base class that defines some common behavior or functionality for a group of related classes. Abstract classes can have both concrete and abstract members, and they can be used as a base class for other classes.
-MARUTHI B M

When to Use an Interface
1. If the problem needs to be solved using multiple inheritances and is composed of different class hierarchies
2. When unrelated classes implement our interface. For example, Comparable provides the compareTo() method that can be overridden to compare two objects
3. When application functionalities have to be defined as a contract, but not concerned about who implements the behavior. i.e., third-party vendors need to implement it fully.
4. Consider using the interface when our problem makes the statement “A is capable of [doing this]”. For example, “Clonable is capable of cloning an object”, “Drawable is capable of drawing a shape”, etc.

When to Use an Abstract Class?
1. When trying to use the inheritance concept in code (share code among many related classes), by providing common base class methods that the child class override.
2. If we have specified requirements and only partial implementation details
3. While classes that extend abstract classes have several common fields or methods (that require non-public modifiers)
4. If one wants to have non-final or non-static methods to modify the states of an object
5. Consider using abstract classes and inheritance when our problem makes the evidence “A is a B”. For example, “Dog is an Animal”, “Lamborghini is a Car”, etc.

More infor: https://www.baeldung.com/java-interface-vs-abstract-class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

DO INTERFACE CAN HAVE A CONSTRUCTOR?

A

No, an interface in C# cannot have a constructor because an interface is a contract or specification that defines the signatures of methods, properties, and events that implementing classes must provide. It does not provide any implementation, including constructors.
–Bhanu

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

CAN YOU CREATE AN INSTANCE OF AN ABSTRACT CLASS OR AN INTERFACE?

A

No, you cannot create an instance of an abstract class or an interface directly in C#.

The purpose of an abstract class is to provide a base class or template for deriving more specialized classes. You cannot create an instance of an abstract class because it is not fully implemented and contains abstract methods that must be implemented by derived classes.

Similarly, An interface does not contain any implementation itself, so it cannot be instantiated. Instead, it serves as a contract that specifies the behavior that an implementing class must adhere to.

To use an abstract class or an interface, you must create a concrete class that derives from the abstract class or implements the interface.

–Sai Krishna

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

WHAT IS BOXING AND UNBOXING?

A

Boxing: Converting value type data into a reference type.
Boxing is an implicit conversion.
Memory location also changes from stack to heap.

Ex: int n=12;
object o = n;

unboxing: Converting Reference type data to value type.
unboxing is an explicit conversion.
Memory location also changes from heap to stack.

ex: n=(int) o

-Yasaswini . G

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are Nullable Types in C#?

A

Nullable types in C# allow you to represent value types, such as int, float, and bool, that can also have a value of null.
Example:
int? nullableInt = null;
float? nullableFloat = 3.14f;
bool? nullableBool = false;

if (nullableInt.HasValue)
{
int value = nullableInt.Value;
Console.WriteLine($”nullableInt has a value of {value}”);
}
else
{
Console.WriteLine(“nullableInt has no value”);
}
–Bhargav Bhat

17
Q

EXPLAIN GENERICS IN C#? WHEN AND WHY TO USE THEM?

A

Generics gives the concept of “ type parameters “ to .Net. Which enables the classes and methods to use a particular data type throughout the execution. Can be achieved using angular brackets

When n Why?
Generic classes and methods combine reusability, type safety, and efficiency.

Generics are most frequently used with collections like lists, dictionaries, stacks, and queues and the methods that operate on them.

(Mahesh)

18
Q

Can you ellaborate on Exceptions in C#?

A

Exceptions in C# are a mechanism to handle runtime errors or exceptional conditions that may occur during the execution of a program. Exceptions enable a programmer to write error-free code and handle errors effectively.

try − A try block identifies a block of code for which particular exceptions is activated. It is followed by one or more catch blocks.

catch −Used to define a catch block. This block catches the exception thrown by the try block.

finally − The finally block is used to execute a given set of statements, whether an exception is thrown or not thrown.

throw − A program throws an exception when a problem shows up. This is done using a throw keyword

-Yugaraj

19
Q

CAN WE EXECUTE MULTIPLE CATCH BLOCKS? Why we need multiple catch blocks?

A

Yes you can have multiple catch blocks with try statement. You start with catching specific exceptions and then in the last block you may catch base Exception . Only one of the catch block will handle your exception. You can have try block without a catch block.

by Ajay

20
Q

What is IEnumerable in C#?

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

Some Key Points:

  • IEnumerable interface contains the System.Collections.Generic namespace.
  • IEnumerable interface is a generic interface that allows looping over generic or non-generic lists.
  • IEnumerable interface also works with linq query expression.
  • IEnumerable interface Returns an enumerator that iterates through the collection.

_Aman Shankar

21
Q

WHAT IS THE DIFFERENCE BETWEEN “OUT” AND “REF” PARAMETERS?

A

The main difference between “ref” and “out” parameters is that “ref” is used to pass a reference to an existing variable, while “out” is used to return a value from a method where the return statement cannot be used. Additionally, “ref” parameters must be initialized before they are passed to the method, while “out” parameters can be uninitialized.
~C. Venkata Teja

22
Q

WHAT IS THE PURPOSE OF “PARAMS” KEYWORD?

A

It is used in method signatures to specify that the method can accept a variable number of arguments of the same type.
with the params keyword, you can pass any number of arguments of the specified type to the method, separated by commas, and the C# compiler will automatically pack them into an array.
Signature Syntax: MethodName(params int[] parameters)

———— MADHU VASANTH ———–

23
Q

WHAT IS A CONSTRUCTOR AND WHAT ARE ITS TYPES?

A

Constructor is a special method used to instantiate objects, In general constructor has same as class name. There are two types 1.Default constructor, 2.Parameterized constructor, 3.Private constructor,4.Copy constructor.
1.Default- A constructor which has no argument is known as default constructor. It is automatically invoked at the time of creating object.
2.Parameterized- Create a constructor which has one or more argument is passed is known as Parameterized constructor.

—- Prasanna

24
Q

WHEN TO USE PRIVATE CONSTRUCTOR?

A

Private constructor is a special instance constructor which is used in a class that contains only static members.

Private constructor is used in singleton class pattern

25
Q

WHAT ARE EXTENSION METHODS IN C#? WHEN TO USE THEM?

A

Extension methods are static method is used to add additional methods without modifying the original class. We can use extension methods anywhere in the application by namespace “using ExtensionMethods”.
Then first parameter inside the method must be preceded with the ‘this’ modifier.
The existing class does not require to recompiling the code.
Adding extension methods to any type is a great way to improve productivity and simplify code and make it readable.
—-Prasanna

26
Q

WHAT YOU MEAN BY DELEGATE? WHEN TO USE THEM?

A
  • A Delegate is a variable that holds the reference to a method or we can say it’s a pointer to a function.
  • A delegate can refer to multiple methods of the same return type and parameters.
  • We can use them when we need to pass a method as a parameter.
    _ Aman Shankar
27
Q

WHAT ARE MULTICAST DELEGATES?

A

The multicast delegate contains a list of the assigned delegates. When the multicast delegate is called, it invokes the delegates in the list, in order. Only delegates of the same type can be combined. The - operator can be used to remove a component delegate from a multicast delegate.
-Yugaraj

28
Q

WHAT ARE ANONYMOUS DELEGATES IN C#?

A

Delegates are reference types and are used to reference any method that has the same signature. In contrast to them, the anonymous functions are all about passing code block or code as a delegate parameter.
-Yugaraj

29
Q

WHAT ARE THE DIFFERENCES BETWEEN EVENTS AND DELEGATES?

A

events and delegates are both used to implement the observer pattern, which is a design pattern.

Delegates are used to create a reference to a method, which can be passed as an argument to another method or stored as a variable.
Events, on the other hand, are a higher-level concept that are built on top of delegates.

The event is a notification mechanism that depends on delegates

An event is dependent on a delegate and cannot be created without delegates.

while delegates are lower-level constructs used to reference methods, events are a higher-level construct that allow objects to respond to interesting actions without needing to know anything about the objects that generate those actions
-sohail.syed

30
Q

WHAT IS “THIS” KEYWORD IN C#? WHEN TO USE IT?

A

What is “This” Keyword?
The this keyword refers to the current instance of the class and is also used as a modifier of the first parameter of an extension method
=> It is often used to differentiate between instance-level and local-level variables, properties, and methods with the same name.

when to use it?
=>To refer to instance-level members
=>To pass the current instance as an argument
=>To call other constructors in the same class
=>To return the current instance from a method
Ex:
using System;
class Program {
public string Name;
public string GetName()
{
return Name;
}
public void SetName(string Name)
{
this.Name = Name;
}
}
class program {
public static void Main()
{
Program obj = new Program();
obj.SetName(“Mounika”);
Console.WriteLine(obj.GetName());
}
}
– Mounika B

31
Q

WHAT IS THE PURPOSE OF “USING” KEYWORD IN C#?

A

The using keyword has two major uses:

The using statement defines a scope at the end of which an object is disposed.
The using directive creates an alias for a namespace or imports types defined in other namespaces.

32
Q

WHAT IS THE DIFFERENCE BETWEEN “IS” AND “AS” OPERATORS?

A

The IS operator is USED TO CHECK the type of an object.

 AS operator is used to PERFORM CONVERSION between compatible reference type.

 The IS operator is of boolean type.
The AS operator is not of boolean type
-sohail.syed

33
Q

WHAT IS THE DIFFERENCE BETWEEN “READONLY” AND “CONSTANT”
VARIABLES ?

A

Constant variables:
- Constant variables can only be initialized at the declaration.
- Their value cannot be changed in the future.
- Therefore, constant variables are used as compile time constants.

Read-only variables:
- Read-only variables can be assigned multiple times depending on the constructor used.
- Therefore, read-only variables are used as run time constants.

–manoj

34
Q

WHAT IS “STATIC” CLASS? WHEN TO USE IT?

A

What is Static Class?

static class is a class that can only contain static data members, static methods, and a static constructor.
=>it is not allowed to create objects of a static class.
=>Static classes are sealed, means you cannot inherit a static class from another class.

when to use it?
1.Utility classes: Static classes are often used to define utility classes that contain helper methods and properties that are not tied to any particular instance of an object.

For example, the Math class in C# is a static class that provides a set of mathematical functions.
2.Singleton pattern: Static classes can also be used to implement the Singleton pattern, which ensures that only one instance of a class can be created throughout the lifetime of an application.
3.Extension methods: Static classes can be used to define extension methods, which allow you to add new functionality to existing classes
without modifying their source code.

  • Mounika B
35
Q

WHAT IS THE DIFFERENCE BETWEEN “VAR” AND “DYNAMIC” IN C#?

A
  • In C#, “var” and “dynamic” are both used to declare variables with implicit typing, but they have different meanings and behaviors.
  • The “var” keyword is used to declare a variable with implicit compile typing, which means that the type of the variable is determined by the compiler based on the value assigned to it. Once a variable is declared with “var”, its type is fixed and cannot be changed at runtime. The use of “var” is mainly useful for simplifying code and avoiding redundancy

var x = 10; // Compiler knows that x is of type int

var s = “Hello”; // Compiler knows that s is of type string

  • the “dynamic” keyword is used to declare a variable with implicit runtime typing. This means that the type of the variable is determined at runtime rather than compile time, and can change during runtime. The use of “dynamic” is mainly useful when working with objects whose type is not known until runtime or when dealing with dynamic languages.

dynamic d = 10; // d is initially of type int

d = “Hello”; // d is now of type string
* Manikandan R

36
Q

What is enum type in c#?

A

In C#, an enum type is a user-defined data type that allows you to define a set of named constants with underlying integral values
Example:
enum DaysOfWeek
{
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday
}
-saiprasad