C# Questions II Flashcards
What are generics in c#.net?
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.
Features of Generics:
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.
Describe the accessibility modifiers in c#.Net
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
What is Virtual Method in C#?
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.
Virtual Method
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.
What is the Difference between Array and ArrayList in C#.Net?
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
What you understand by Value types and Reference types in C#.Net?
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.
What is Serialization?
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).
What is the use of using statement in C#?
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.
What is jagged array in C#.Net?
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.
What is Multithreading with .NET?
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.
Explain Anonymous type in C#?
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
Explain Hashtable in C#?
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).
What is LINQ in C#?
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
What is File Handling in C#.Net?
The System.IO namespace provides four classes that allow you to manipulate individual files, as well as interact with a machine directory structure.
What is Reflection in C#.Net?
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.
What is Expression Trees In C#?
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.
Differences between Object, Var and Dynamic type?
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 can you implement multiple inheritance in C#?
Using Interfaces.
Are private class members inherited to the derived class?
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.
When and why to use method overloading
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.