Index

C# tagged Questions - Page 1

What is .NET Framework?


The .NET Framework has two main components: the common language runtime and the .NET Framework class library.

What is the extension of multifile assembly?


.netmodule

What is Reflection in C#.Net with Example?


Reflection is a concept of reading the metadata at runtime. System.Reflection.InvokeMember is used to invoke a method dynamically at runtime. Once you have an object reference to the assembly of interest, you can call the GetTypes method on that assembly, which returns an array of all the types in that assembly. You can use control logic to identify more specific types in that array, and use iterating logic to parse your array, returning the type information to the user if needed. The ability to retrieve type information can be useful for determining alternative types you could use for a given task, or identifying existing elements which could provide you with the functionality you need. The first thing to learn when retrieving types from a particular assembly is, how to identify an assembly. The first is by identifying a particular object that you want to find the assembly of, and requesting the assembly for the module of that object (remember that a module is a logical grouping of types and code, such as a .dll or .exe). The second is by using the LoadFrom method of the Assembly class to load a specific assembly for a named module (such as mypgm.exe).

class CMain
 
 {
 
     static void Main(string[] args)
 
     {
 
         //Step 1 : Load an Assembly
 
         Assembly asm = Assembly.LoadFrom("C:\\Karthik Documents\\MathLibrary.dll");
 
         //Step 2 : Get the Modules
 
         Module[] modules = asm.GetModules();
 
 
 
         foreach (Module mod in modules)
 
         {
 
             Console.WriteLine("module {0}", mod.Name);
 
             //Step 3 : Get types
 
             Type[] types = mod.GetTypes();
 
 
 
             foreach (Type t in types)
 
             {
 
                 Console.WriteLine("Type {0}", t.Name);
 
                 //Step 4 : Get Methods
 
                 MethodInfo[] methods = t.GetMethods();
 
 
 
                 foreach (MethodInfo method in methods)
 
                 {
 
                     Console.WriteLine("method : {0}", method.Name);
 
                 }
 
                 FieldInfo[] fields = t.GetFields();
 
                 foreach (FieldInfo field in fields)
 
                 {
 
                     Console.WriteLine("field : {0}", field.Name);
 
                 }
 
             }
 
         }
 
     }
 
 }

How to get distinct values from an array in C#?


[C#]

public int[] GetDistinctValues(int[] arr)

{
List<int> lst = new List<int>();
for (int i = 0; i < arr.Length; i++)
{
if (lst.Contains(arr))
continue;
lst.Add(arr);
}
return lst.ToArray();
}

How many languages .NET is supporting now?


When .NET was introduced it came with several languages. VB.NET, C#, COBOL and Perl, etc. 44 languages are supported.

What is polymorphism?


In object-oriented programming, the term "polymorphism" refers to the ability of objects to take the form objects of difference classes.

What are the OOPS concepts?


1) Encapsulation: It is the mechanism that binds together code and data in manipulates, and keeps both safe from outside interference and misuse. In short it isolates a particular code and data from all other codes and data. A well-defined interface controls the access to that particular code and data.

2) Inheritance: It is the process by which one object acquires the properties of another object. This supports the hierarchical classification. Without the use of hierarchies, each object would need to define all its characteristics explicitly. However, by use of inheritance, an object need only define those qualities that make it unique within its class. It can inherit its general attributes from its parent. A new sub-class inherits all of the attributes of all of its ancestors.

3) Polymorphism: It is a feature that allows one interface to be used for general class of actions. The specific action is determined by the exact nature of the situation. In general polymorphism means "one interface, multiple methods", This means that it is possible to design a generic interface to a group of related activities. This helps reduce complexity by allowing the same interface to be used to specify a general class of action. It is the compiler's job to select the specific action (that is, method) as it applies to each situation.

What is the difference between a Struct and a Class?


The struct type is suitable for representing lightweight objects such as Point, Rectangle, and Color. Although it is possible to represent a point as a class, a struct is more efficient in some scenarios. For example, if you declare an array of 1000 Point objects, you will allocate additional memory for referencing each object. In this case, the struct is less expensive.

When you create a struct object using the new operator, it gets created and the appropriate constructor is called. Unlike classes, structs can be instantiated without using the new operator. If you do not use new, the fields will remain unassigned and the object cannot be used until all of the fields are initialized.

It is an error to declare a default (parameterless) constructor for a struct. A default constructor is always provided to initialize the struct members to their default values.

It is an error to initialize an instance field in a struct.

There is no inheritance for structs as there is for classes. A struct cannot inherit from another struct or class, and it cannot be the base of a class. Structs, however, inherit from the base class Object. A struct can implement interfaces, and it does that exactly as classes do.

A struct is a value type, while a class is a reference type.

What is a delegate?


A delegate object encapsulates a reference to a method. In C++ they were referred to as function pointers.

Multicast delegate:
It's a delegate that points to and eventually fires off several methods.

What are the OOPS concepts?


Encapsulation:
It is the mechanism that binds together code and data in manipulates, and keeps both safe from outside interference and misuse. In short it isolates a particular code and data from all other codes and data. A well-defined interface controls the access to that particular code and data.

Inheritance:
It is the process by which one object acquires the properties of another object. This supports the hierarchical classification. Without the use of hierarchies, each object would need to define all its characteristics explicitly. However, by use of inheritance, an object need only define those qualities that make it unique within its class. It can inherit its general attributes from its parent. A new sub-class inherits all of the attributes of all of its ancestors.

Polymorphism:
It is a feature that allows one interface to be used for general class of actions. The specific action is determined by the exact nature of the situation. In general polymorphism means "one interface, multiple methods", This means that it is possible to design a generic interface to a group of related activities. This helps reduce complexity by allowing the same interface to be used to specify a general class of action. It is the compiler's job to select the specific action (that is, method) as it applies to each situation.

What is the difference between a constructor and a method?


A constructor is a member function of a class that is used to create objects of that class. It has the same name as the class itself, has no return type, and is invoked using the new operator.

A method is an ordinary member function of a class. It has its own name, a return type (which may be void), and is invoked using the dot operator.

Difference between value types and reference types in .NET


Most programming languages provide built-in data types, such as integers and floating-point numbers, that are copied when they are passed as arguments (that is, they are passed by value). In the .NET Framework, these are called value types. The run-time supports two kinds of value types:

Built-in value types:
The .NET Framework defines built-in value types, such as System.Int32 and System.Boolean, which correspond and are identical to primitive data types used by programming languages.

User-defined value types:
Your language will provide ways to define your own value types, which derive from System.ValueType. If you want to define a type representing a value that is small, such as a complex number (using two floating-point numbers), you might choose to define it as a value type because you can pass the value type efficiently by value. If the type you are defining would be more efficiently passed by reference, you should define it as a class instead.
Variables of reference types, referred to as objects, store references to the actual data.

Following are examples of the reference types:
class
interface
delegate

Following are the built-in reference types:
object
string

What is the output of the following C# code?


main()

{
int a = 20;
a++;
a += a--;
Console.WriteLine(a);
}

42. The increment operator (++) increments its operand by 1. The increment operator can appear before or after its operand. The decrement operator (--) decrements its operand by 1. The decrement operator can appear before or after its operand: += Operator. The addition assignment operator.

Write a program to reverse a linked list


Implementation of Iterative Method:

#include<stdio.h>

#include<stdlib.h>

/* Link list node */
struct node
{
int data;
struct node* next;
};

/* Function to reverse the linked list */
static void reverse(struct node** head_ref)
{
struct node* prev = NULL;
struct node* current = *head_ref;
struct node* next;
while (current != NULL)
{
next = current->next;
current->next = prev;
prev = current;
current = next;
}
*head_ref = prev;
}

/* Function to push a node */
void push(struct node** head_ref, int new_data)
{
/* allocate node */
struct node* new_node =
(struct node*) malloc(sizeof(struct node));

/* put in the data */
new_node->data = new_data;

/* link the old list off the new node */
new_node->next = (*head_ref);

/* move the head to point to the new node */
(*head_ref) = new_node;
}

/* Function to print linked list */
void printList(struct node *head)
{
struct node *temp = head;
while(temp != NULL)
{
printf("%d ", temp->data);
temp = temp->next;
}
}

/* Drier program to test above function*/
int main()
{
/* Start with the empty list */
struct node* head = NULL;

push(&head, 20);
push(&head, 4);
push(&head, 15);
push(&head, 85);

printList(head);
reverse(&head);
printf("\n Reversed Linked list \n");
printList(head);
getchar();
}

How can we avoid the exception raised when arithmetic overflow occurs within a block


By using unchecked keyword. Basically it is used for the prevention of overflow checking.

Is there any alternative to achieve multiple inheritance in C#?


By using Interfaces, multiple inheritance could be achieved in C#.

What is a partial class? How to declare?


Partial classes can be defined in more than one location and can have different members. So you
can save partial classes in different physical locations, but logically when you create the object they will act like one class.

public partial class ClassName

Which operators can be overloaded?


Only Unary, Binary and Comparison operators can be overloaded.

Which C# keyword can be applied to a class to prevent it from being used as a base class?


Sealed. C# uses the sealed keyword to prevent inheritance. VB.Net used the term NotInheritance and Java uses the term final for this same concept.

Which is used to access attributes at runtime?


Reflection. It is concept of accessing metadata at runtime.

Similar Tags