C# Questions II Flashcards

1
Q

What are generics in c#.net?

A

Generics allow you to delay the specification of the data type of programming elements in a class or a method, until it is actually used in the program.

In other words, generics allow you to write a class or method that can work with any data type. You write the specifications for the class or the method, with substitute parameters for data types. When the compiler encounters a constructor for the class or a function call for the method, it generates code to handle the specific data type.

Generic classes and methods combine reusability, type safety and efficiency in a way that their non-generic counterparts cannot.

Generics are most frequently used with collections and the methods that operate on them. Version 2.0 of the .NET Framework class library provides a new namespace,

System.Collections.Generic, which contains several new generic-based collection classes. It is recommended that all applications that target the .NET Framework 2.0 and later use the new generic collection classes instead of the older non-generic counterparts such as ArrayList.

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

Features of Generics:

A

Generics is a technique that enriches your programs in the following ways:

  • It helps you to maximize code reuse, type safety and performance.
  • You can create generic collection classes. The .NET Framework class library contains several new generic collection classes in the

System.Collections.Generic namespace. You may use these generic collection classes instead of the collection classes in the System.Collections namespace.

  • You can create your own generic interfaces, classes, methods, events and delegates.
  • You may create generic classes constrained to enable access to methods on specific data types.
  • You may get information on the types used in a generic data type at run-time using reflection.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Describe the accessibility modifiers in c#.Net

A

They support the concept of encapsulation, which promotes the idea of hiding functionality.

Public
There are no restrictions on accessing public members

Private
Access is limited to within the class definition. This is the default access modifier type if none is formally specified

Protected
Access is limited to within the class definition and any class that inherits from the class

Internal
Access is limited exclusively to classes defined within the current project assembly

Protected internal
Access is limited to current assembly

Protected private: Access is limited to the containing class or types derived from the containing class within the current assembly

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

What is Virtual Method in C#?

A

A virtual method is a method that can be redefined in derived classes.

A virtual method has an implementation in a base class as well as derived the class.

It is used when a method’s basic functionality is the same but sometimes more functionality is needed in the derived class.** A virtual method is created in the base class that can be overridden in the derived class. **We create a virtual method in the base class using the virtual keyword and that method is overridden in the derived class using the override keyword.

When a virtual method is invoked, the run-time type of the object is checked for an overriding member. The overriding member in the most derived class is called, which might be the original member, if no derived class has overridden the member.

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

Virtual Method

A

By default, methods are non-virtual.

We can’t override a non-virtual method.

We can’t use the virtual modifier with the static, abstract, private or override modifiers.

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

What is the Difference between Array and ArrayList in C#.Net?

A

Array uses the vector array to store the elements
ArrayList uses the linked list to store the elements

Size of the array must be defined until redeem used(vb)
ArrayList: No need to specify the storage size

Array is specific data type storage
ArrayList can be stored everything as object

Array: No need to do the typecasting
ArrayList: Every time type casting has to do

Array: It will not lead to runtime exception
ArrayList: It leads to the runtime error exception

Array: Element cannot be inserted or deleted in between
ArrayList: Elements can be inserted and deleted

Array: There is no build in members to do ascending or descending
Arraylist has many methods to do operation like sort insert, remove, binarysearch, etc

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

What you understand by Value types and Reference types in C#.Net?

A

In C# data types can be of two types: Value Types and Reference Types.

Value type variables contain their object (or data) directly. If we copy one value type variable to another then we are actually making a copy of the object for the second variable. Both of them will independently operate on their values,

Value Type member will locate into Stack and reference member will locate in Heap always.

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

What is Serialization?

A

Serialization means saving the state of your object to secondary memory, such as a file.

Here comes Serialization. You will serialize all your necessary business classes and save them into a text or XML file, on your hard disk. So you can easily test your desired result by comparing your serialized saved data with, your desired output data. You can say it is a little bit of autonomic unit testing performed by the developer.

There are three types of serialization:

  • Binary serialization (Save your object data into binary format).
  • Soap Serialization (Save your object data into binary format; mainly used in network related communication).

* Xml Serialization (Save your object data into an XML file).

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

What is the use of using statement in C#?

A

The .Net Framework provides resource management for managed objects through the garbage collector -

You do not have to explicitly allocate and release memory for managed objects. Clean-up operations for any unmanaged resources should perform in the destructor in C#. To allow the programmer to explicitly perform these clean-up activities, objects can provide a Dispose method that can be invoked when the object is no longer needed.

The using statement in C# defines a boundary for the object outside of which, the object is automatically destroyed. The using statement is excited when the end of the “using” statement block or the execution exits the “using” statement block indirectly, for example - an exception is thrown. The “using” statement allows you to specify multiple resources in a single statement. The object could also be created outside the “using” statement.

The objects specified within the using block must implement the IDisposable interface. The framework invokes the Dispose method of objects specified within the “using” statement when the block is exited.

MUST IMPLEMENT IDISPOSABLE

To automatically release unmanaged resources (like file handles, database connections, streams) when they are no longer needed, without manually calling the Dispose() method.

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

What is jagged array in C#.Net?

A

A jagged array is an array whose elements are arrays. The elements of a jagged array can be of different dimensions and sizes. A jagged array is sometimes called an “array of arrays.” A special type of array is introduced in C#. A Jagged Array is an array of an array in which the length of each array index can differ.

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

What is Multithreading with .NET?

A

The real usage of a thread is not about a single sequential thread, but rather using multiple threads in a single program.

Multiple threads running at the same time and performing various tasks is referred as
Multithreading.

A thread is considered to be a lightweight process because it runs within the context of a program and takes advantage of resources allocated for that program.

A single-threaded process contains only one thread while a multithreaded process contains more than one thread for execution.

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

Explain Anonymous type in C#?

A

Anonymous types allow us to create new type without defining them.

This is way to defining read only properties into a single object without having to define type explicitly. Here Type is generating by the compiler and it is accessible only for the current block of code. The type of properties is also inferred by the compiler.

We can create anonymous types by using “new” keyword together with the object initializer.

var person = new { Name = “Alice”, Age = 30 };

they are readonly

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

Explain Hashtable in C#?

A

A Hashtable is a collection that stores (Keys, Values) pairs.

The hash table is a general-purpose dictionary collection. Each item within the collection is a DictionaryEntry object with two properties: a key object and a value object. These are known as Key/Value. When items are added to a hash table, a hashcode is generated automatically.

Hashtable is a non-generic collection,
Each key in the Hashtable must be unique.
The elements in a Hashtable are not stored in any specific order.
The keys are hashed to determine their position in the internal storage of the Hashtable, which makes key lookups very fast (usually O(1) time complexity for accessing elements).

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

What is LINQ in C#?

A

LINQ stands for Language Integrated Query. LINQ is a data querying methodology which provides querying capabilities to .NET languages with syntax similar to a SQL query

LINQ has a great power of querying on any source of data. The data source could be collections of objects, database or XML files. We can easily retrieve data from any object that implements the IEnumerable interface.

Advantages of LINQ:

  • LINQ offers an object-based, language-integrated way to query over data no matter where that data came from. So through LINQ we can query database, XML as well as collections.

* Compile time syntax checking.

  • It allows you to query collections like arrays, enumerable classes etc. in the native language of your application, and like VB or C# in much the same way as you would 66 query a database using SQL.

nazwaListy.Where/Select/OrderBy/GropuBY.CallBackFunction

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

What is File Handling in C#.Net?

A

The System.IO namespace provides four classes that allow you to manipulate individual files, as well as interact with a machine directory structure.

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

What is Reflection in C#.Net?

A

Reflection in C# is a feature that allows a program to inspect and manipulate its own structure and metadata at runtim

Reflection is a feature in C# (and other .NET languages) that allows you to inspect and interact with the types, methods, properties, fields, and other metadata of objects at runtime.
Reflection is part of the System.Reflection namespace in .NET, and it allows you to:

Get information about types (classes, interfaces, etc.).
Create instances of types dynamically.
Invoke methods and access properties/fields of objects dynamically.
Modify fields or properties of objects at runtime.
Work with attributes attached to types and members.

Reflection typically is the process of runtime type discovery to inspect metadata, CIL code, late binding and self-generating code. At run time by using reflection, we can access the same “type” information as displayed by the ildasm utility at design time. The reflection is analogous to reverse engineering in which we can break an existing *.exe or *.dll assembly to explore defined significant contents information, including methods, fields, events and properties.

You can dynamically discover the set of interfaces supported by a given type using the System.Reflection namespace.

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

What is Expression Trees In C#?

A

Expression and Expression<> are basically classes that can represent the CSharp code as Data.

Expression Trees in C# are a powerful feature that allows you to represent code as data. An expression tree is a data structure that represents code in a tree-like format, where each node in the tree is an expression (e.g., addition, method call, or variable access). You can build, manipulate, and execute these expressions at runtime.

Unlike Func<> or Action<> Expressions are non-compiled Data about the code. Most of LINQ Providers has been built using Expressions. Walkthrough of a sample expression: Expression can be parsed, analyzed in the program.

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

Differences between Object, Var and Dynamic type?

A

Object * Object was introduced with C# 1.0 * It can store any kind of value, because object is the base class of all type in .NET framework. * Compiler has little information about the type. * Object type can be passed as method argument and method also can return object type. * Need to cast object variable to original type to use it and performing desired operations. * Cause the problem at run time if the stored value is not getting converted to underlying data type. * Useful when we don’t have more information about the data type.

Var * Var was introduced with C# 3.0 * It can store any type of value but It is mandatory to initialize var types at the time of declaration. * It is type safe i.e. Compiler has all information about the stored value, so that it doesn’t cause any issue at run-time. * Var type cannot be passed as method argument and method cannot return object type. Var type work in the scope where it defined. * No need to cast because compiler has all information to perform operations. * Doesn’t cause problem because compiler has all information about stored value. * Useful when we don’t know actual type i.e. type is anonymous.

Dynamic * Dynamic was introduced with C# 4.0 * It can store any type of the variable, similar to old VB language variable. * It is not type safe i.e. Compiler doesn’t have any information about the type of variable. * Dynamic type can be passed as method argument and method also can return dynamic type. * Casting is not required but you need to know the properties and methods related to stored type. * Cause problem if the wrong properties or methods are accessed because all the information about stored value is get resolve only at run time. * Useful when we need to code using reflection or dynamic languages or with the COM objects, because you need to write less code.

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

How can you implement multiple inheritance in C#?

A

Using Interfaces.

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

Are private class members inherited to the derived class?

A

Yes, the private members are also inherited in the derived class but we will not be able to access them. Trying to access a private base class member in the derived class will report a compile time error.

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

When and why to use method overloading

A

Use method overloading in situation where you want a class to be able to do something, but there is more than one possibility for what information is supplied to the method that carries out the task.

You should consider overloading a method when you for some reason need a couple of methods that take different parameters, but conceptually do the same thing. Method overloading showing many forms.

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

Method Overriding:

A

Whereas Overriding means changing the functionality of a method without changing the signature. We can override a function in base class by creating a similar function in derived class. This is done by using virtual/overrides keywords. Base class method has to be marked with virtual keyword and we can override it in derived class using override keyword. Derived class method will completely overrides base class method i.e. when we refer base class object created by casting derived class object a method in derived class will be called.

23
Q

Does C# support multiple-inheritance?

A

No. But you can use Interfaces.

24
Q

Where is a protected class-level variable available?

A

It is available to any sub-class derived from base c las

25
Q

Are private class-level variables inherited?

A

Yes, but they are not accessible.

26
Q

Describe the accessibility modifier “protected internal”.

A

It is available to classes that are within the same assembly and derived from the specified base class.

27
Q

Which class is at the top of .NET class hierarchy?

A

System.Object.

28
Q

What does the term immutable mean?

A

The data value may not be changed.

Note: The variable value may be changed, but the original immutable data value was discarded and a new data value was created in memory.

29
Q

What’s the difference between the System.Array.CopyTo() and System.Array.Clone()?

A

The Clone() method returns a new array (a shallow copy) object containing all the elements in the original array. The CopyTo() method copies the elements into another existing array. Both perform a shallow copy. A shallow copy means the contents (each array element) contains references to the same object as the elements in the original array. A deep copy (which neither of these methods performs) would create a new instance of each element’s object, resulting in a different, yet identacle object.

30
Q

How can you sort the elements of the array in descending order?

A

By calling Sort() and then Reverse() methods.

31
Q

What’s the .NET collection class that allows an element to be accessed using a unique key?

A

HashTable.

32
Q

Can you prevent your class from being inherited by another class?

A

Yes. The keyword “sealed” will prevent the class from being inherited.

33
Q

Can you allow a class to be inherited, but prevent the method from being over-ridden?

A

Yes. Just leave the class public and make the method sealed.

34
Q

When do you absolutely have to declare a class as abstract?

A

When the class itself is inherited from an abstract class, but not all base abstract methods have been overridden.

When at least one of the methods in the class is abstract.

35
Q

What’s the implicit name of the parameter that gets passed into the set method/property of a class?

A

Value. The data type of the value parameter is defined by whatever data type the property is declared .

36
Q

What are the different ways a method can be overloaded?

A

Different parameter data types, different number of parameters, different order of parameters.

37
Q

If a base class has a number of overloaded constructors, and an inheriting class has a number of overloaded constructors; can you enforce a call from an inherited constructor to a specific base constructor?

A

Yes, just place a colon, and then keyword base (parameter list to invoke the appropriate constructor) in the overloaded constructor definition inside the inherited class.

38
Q

What’s the implicit name of the parameter that gets passed into the class’ set method?

A

Value, and it’s datatype depends on whatever variable we’re changing.

39
Q

Does C# support multiple inheritance?

A

No, use interfaces instead.

40
Q

When you inherit a protected class-level variable, who is it available to?

A

Classes in the same namespace.

41
Q

Describe the accessibility modifier protected internal.?

A

It’s available to derived classes and classes within the same assembly(and naturally from the base class it’s declared in)

Assembly zawiera skompilowany kod (w języku Intermediate Language, IL), który jest uruchamiany przez środowisko uruchomieniowe .NET (CLR).

Zawiera także metadane opisujące typy, metody, właściwości oraz inne elementy wewnątrz pliku.

Może także zawierać zasoby takie jak obrazy, pliki tekstowe lub dane potrzebne w aplikacji.

42
Q

C# provides a default constructor for me. I write a constructor that takes a string as a parameter, but want to keep the no parameter one. How many constructors should I write?

A

Two. Once you write at least one constructor, C# cancels the freebie constructor, and now you have to write one yourself, even if there’s no implementation in it.

43
Q

What does the keyword virtual mean in the method definition?

A

The method can be over-ridden.

44
Q

Can you declare the override method static while the original method is non-static?

A

No, you can’t, the signature of the virtual method must remain the same, only the keyword virtual is changed to keyword override.

45
Q

Can you override private virtual methods?

A

No, moreover, you cannot access private methods in inherited classes, have to be protected in the base class to allow any sort of access.

46
Q

When do you absolutely have to declare a class as abstract (as opposed to free-willed educated choice or decision based on UML diagram)?

A

When at least one of the methods in the class is abstract. When the class itself is inherited from an abstract class, but not all base abstract methods have been over-ridden.

47
Q

Why can’t you specify the accessibility modifier for methods inside the interface?

A

They all must be public. Therefore, to prevent you from getting the false impression that you have any freedom of choice, you are not allowed to specify any accessibility, it’s public by default.

48
Q

What is the difference between ToString() and Convert.ToString()?

A

ToString() doesnot handle null values

but

Convert.ToString() will handle null values (returns empty string “”)

49
Q

What is the Difference between int.Parse() and Convert.ToInt32()?

A

int.Parse() will convert only string to int

Convert.ToInt32() is used to convert any datatype to int type

50
Q

What is the difference between Array and Collections?

A

We need to specify the size of the Array at the time of declaration but while Working with collections it is not required to mention the size of the Collection because the size of the collection will be fixed at runtime –>The members of the Array must be of same datatype but collections can have elements of Different datatypes

51
Q

What is OverLoading?

A

same method name+Different method signature in same class

52
Q

What is Overriding?

A

same method heading+different method body in base class and derived class

53
Q
A