[go: up one dir, main page]

0% found this document useful (0 votes)
6 views9 pages

C# (1)

The document provides a comprehensive overview of C# programming concepts, including definitions and explanations of key terms such as C#, object, constructors, method overloading, and exceptions. It covers various topics like access modifiers, value types vs. reference types, and the differences between arrays and ArrayLists. Additionally, it discusses advanced topics such as serialization, delegates, and the principles of object-oriented programming.

Uploaded by

yahsuD
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views9 pages

C# (1)

The document provides a comprehensive overview of C# programming concepts, including definitions and explanations of key terms such as C#, object, constructors, method overloading, and exceptions. It covers various topics like access modifiers, value types vs. reference types, and the differences between arrays and ArrayLists. Additionally, it discusses advanced topics such as serialization, delegates, and the principles of object-oriented programming.

Uploaded by

yahsuD
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 9

1. What is C#?

C# is an object oriented, type safe and managed language that is compiled by .Net framework to
generate Microsoft Intermediate Language.
3. Can multiple catch blocks be executed?
No, Multiple catch blocks can’t be executed. Once the proper catch code executed, the control is
transferred to the finally block and then the code that follows the finally block gets executed.
4. What is the difference between public, static and void?
All these are access modifiers in C#. Public declared variables or methods are accessible anywhere
in the application. Static declared variables or methods are globally accessible without creating an
instance of the class. The compiler stores the address of the method as the entry point and uses
this information to begin execution before any objects are created. And Void is a type modifier that
states that the method or variable does not return any value.
5. What is an object?
An object is an instance of a class through which we access the methods of that class. “New”
keyword is used to create an object. A class that creates an object in memory will contain the
information about the methods, variables and behavior of that class.
6. Define Constructors?
A constructor is a member function in a class that has the same name as its class. The constructor
is automatically invoked whenever an object class is created. It constructs the values of data
members while initializing the class.
7. What is Jagged Arrays?
The array which has elements of type array is called jagged array. The elements can be of different
dimensions and sizes. We can also call jagged array as Array of arrays.
8. What is the difference between ref & out parameters?
An argument passed as ref must be initialized before passing to the method whereas out
parameter needs not to be initialized before passing to a method.
9. What is the use of using statement in C#?
The using block is used to obtain a resource and use it and then automatically dispose of when the
execution of block completed.
10. What is serialization?
When we want to transport an object through network then we have to convert the object into a
stream of bytes. The process of converting an object into a stream of bytes is called Serialization.
For an object to be serializable, it should inherit ISerialize Interface.
De-serialization is the reverse process of creating an object from a stream of bytes.
11. Can “this” be used within a static method?
We can’t use ‘This’ in a static method because we can only use static variables/methods in a static
method.
12. What is difference between constants and read-only?
Constant variables are declared and initialized at compile time. The value can’t be changed after
wards. Read-only variables will be initialized only from the Static constructor of the class. Read
only is used only when we want to assign the value at run time.
13. What is an interface class?
Interface is an abstract class which has only public abstract methods and the methods only have
the declaration and not the definition. These abstract methods must be implemented in the
inherited classes.
14. What are value types and reference types?
Value Type:
A Value Type stores its contents in memory allocated on the stack. When you created a Value Type,
a single space in memory is allocated to store the value and that variable directly holds a value. If
you assign it to another variable, the value is copied directly and both variables work
independently. Predefined datatypes, structures, enums are also value types, and work in the
same way. Value types can be created at compile time and Stored in stack memory, because of
this, Garbage collector can't access the stack.

int x = 10;
Reference Type:
Reference Types are used by a reference which holds a reference (address) to the object but not
the object itself. Because reference types represent the address of the variable rather than the
data itself, assigning a reference variable to another doesn't copy the data. Instead it creates a
second copy of the reference, which refers to the same location of the heap as the original value.
Reference Type variables are stored in a different area of memory called the heap. This means that
when a reference type variable is no longer used, it can be marked for garbage collection.
Examples of reference types are Classes, Objects, Arrays, Indexers, Interfaces etc.
e.g.
int[] iArray = new int[20];
In the above code the space required for the 20 integers that make up the array is allocated on the
heap.

16. What are sealed classes in C#?


We create sealed classes when we want to restrict the class to be inherited. Sealed modifier used
to prevent derivation from a class. If we forcefully specify a sealed class as base class then a
compile-time error occurs.
17. What is method overloading?
Method overloading is creating multiple methods with the same name with unique signatures in
the same class. When we compile, the compiler uses overload resolution to determine the specific
method to be invoke.
18. What is the difference between Array and Arraylist?
In an array, we can have items of the same type only. The size of the array is fixed. An arraylist is
similar to an array but it doesn’t have a fixed size.
19. Can a private virtual method be overridden?
No, because they are not accessible outside the class.
20. Describe the accessibility modifier “protected internal”.
Protected Internal variables/methods are accessible within the same assembly and also from the
classes that are derived from this parent class.
21. What are the differences between System.String and System.Text.StringBuilder
classes?
The String object is immutable. Every time you use one of the methods in the System.String class,
you create a new string object in memory, which requires a new allocation of space for that new
object. In situations where you need to perform repeated modifications to a string, the overhead
associated with creating a new String object can be costly. The System.Text.StringBuilder class can
be used when you want to modify a string without creating a new object. For example, using the
StringBuilder class can boost performance when concatenating many strings together in a loop.
22. What’s the difference between the System.Array.CopyTo() and System.Array.Clone()
?
Using Clone() method, we creates a new array object containing all the elements in the original
array and using CopyTo() method, all the elements of existing array copies into another existing
array. Both the methods perform a shallow copy.
23. How can we sort the elements of the array in descending order?
Using Sort() methods followed by Reverse() method.
24. Write down the C# syntax to catch exception?
To catch an exception, we use try catch blocks. Catch block can have parameter of
system.Exception type.
Eg:
[csharp]try
{
GetAllData();
}
catch(Exception ex)
{
}[/csharp]
In the above example, we can omit the parameter from catch statement.
25. What’s the difference between an interface and abstract class?
Interfaces have all the methods having only declaration but no definition. In an abstract class, we
can have some concrete methods. In an interface class, all the methods are public. An abstract
class may have private methods.
26. What is the difference between Finalize() and Dispose() methods?
Finalize:
1. Finalize() belongs to the Object class.
2. It is automatically called by the Garbage Collection mechanism when the object goes out of the
scope(usually at the end of the program
3. It is slower method and not suitable for instant disposing of the objects.
4. It is non-deterministic function i.e., it is uncertain when Garbage Collector will call Finalize()
method to reclaim memory.
Dispose:

1. Dispose() belongs to the IDisposable interface


2. We have to manually write the code to implement it(User Code)
ex: if we have emp class we have to inherit it from the IDisposable interface
and write code. We may have to suppress the Finalize method using
GC.SuppressFinalize() method.
3. Faster method for instant disposal of the objects.
4. It is deterministic function as Dispose() method is explicitly called by the User Code.
27. What are circular references?
Circular reference is situation in which two or more resources are interdependent on each other
causes the lock condition and make the resources unusable.
30. List down the commonly used types of exceptions in .Net?
ArgumentException, ArgumentNullException , ArgumentOutOfRangeException,
ArithmeticException, DivideByZeroException ,OverflowException ,
IndexOutOfRangeException ,InvalidCastException ,InvalidOperationException ,
IOEndOfStreamException , NullReferenceException , OutOfMemoryException ,
StackOverflowException etc.
31. What are Custom Exceptions?
Sometimes there are some errors that need to be handeled as per user requirements. Custom
exceptions are used for them and are used defined exceptions.
32. What are delegates?
Delegates are same are function pointers in C++ but the only difference is that they are type safe
unlike function pointers. Delegates are required because they can be used to write much more
generic type safe functions.
33. How do you inherit a class into other class in C#?
Colon is used as inheritance operator in C#. Just place a colon and then the class name.
[csharp] public class DerivedClass : BaseClass[/csharp]

34. What is the base class in .net from which all the classes are derived from?
[csharp]System.Object[/csharp]
35. What is the difference between method overriding and method overloading?
Overloading: is the mechanism to have more than one method with same name but with
different signature (parameters). A method can be overloaded on the basis of following properties
1. Have different number of parameter
2. Having same number of parameters but of different type
3. Having same number and type of parameters but in different orders
A method cannot be overloaded on the basis of
1. Different return type
2. Different access modifier
3. Normal and optional parameters

n the same way we can overload constructor of a class by defining more than one constructor with
different parameters which is known as constructor overloading.
Overriding: Overriding can be done in derived class, an override method provides a new
implementation of a method inherited from parent class.
To override a method in base (parent) class it must be
1. virtual
2. abstract
3. override
We cannot override a base method which is in base class as
1. non-virtual
2. static
We cannot use the following modifiers to modify an override method in derived class
1. new
2. static
3. virtual
4. abstract

Conclusion:
 Overloading can be done in same class
 Overriding can be done in parent and derived class
 Overloading in used when we need same method in same class with different parameters
 Overriding is used when we need to redefine a method that has already been defined in
parent class (using the exact same signature
 Overloading is resolved at compile time
 Overriding is resolved at run time

36. What are the different ways a method can be overloaded?


Methods can be overloaded using different data types for parameter, different order of
parameters, and different number of parameters.
37. Why can’t you specify the accessibility modifier for methods inside the interface?
In an interface, we have virtual methods that do not have method definition. All the methods are
there to be overridden in the derived class. That’s why they all are public.
38. How can we set class to be inherited, but prevent the method from being over-
ridden?
Declare the class as public and make the method sealed to prevent it from being overridden.
39. What happens if the inherited interfaces have conflicting method names?
Implement is up to you as the method is inside your own class. There might be problem when the
methods from different interfaces expect different data, but as far as compiler cares you’re okay.
40. What is the difference between a Struct and a Class?
Structs are value-type variables and classes are reference types. Structs stored on the stack,
causes additional overhead but faster retrieval. Structs cannot be inherited.
41. How to use nullable types in .Net?
Value types can take either their normal values or a null value. Such types are called nullable
types.
[csharp]Int? someID = null;
If(someID.HasVAlue)
{
}
[/csharp]
42. How we can create an array with non-default values?
We can create an array with non-default values using Enumerable.Repeat.
43. What is difference between is and as operators in c#?
“is” operator is used to check the compatibility of an object with a given type and it returns the
result as Boolean.
“as” operator is used for casting of object to a type or a class.
48. How to implement singleton design pattern in C#?
In singleton pattern, a class can only have one instance and provides access point to it globally.
Eg:
[csharp]
Public sealed class Singleton
{
Private static readonly Singleton _instance = new Singleton();
}
[/csharp]
49. What is the difference between directcast and ctype?
DirectCast is used to convert the type of an object that requires the run-time type to be the same
as the specified type in DirectCast.
Ctype is used for conversion where the conversion is defined between the expression and the
type.
50. Is C# code is managed or unmanaged code?
C# is managed code because Common language runtime can compile C# code to Intermediate
language.

What is a Destructor?
A Destructor has the same name as the class with a tilde character and is used to destroy an
instance of a class.

Can a class have more than 1 destructor?


No, a class can have only 1 destructor.

Can structs in C# have destructors?


No, structs can have constructors but not destructors, only classes can have destructors.

Can you pass parameters to destructors?


No, you cannot pass parameters to destructors. Hence, you cannot overload destructors.

Can you explicitly call a destructor?


No, you cannot explicitly call a destructor. Destructors are invoked automatically by the garbage
collector.

Why is it not a good idea to use Empty destructors?


When a class contains a destructor, an entry is created in the Finalize queue. When the destructor
is called, the garbage collector is invoked to process the queue. If the destructor is empty, this just
causes a needless loss of performance.
s it possible to force garbage collector to run?
Yes, it possible to force garbage collector to run by calling the Collect() method, but this is not
considered a good practice because this might create a performance over head. Usually the
programmer has no control over when the garbage collector runs. The garbage collector checks
for objects that are no longer being used by the application. If it considers an object eligible for
destruction, it calls the destructor(if there is one) and reclaims the memory used to store the
object.

Usually in .NET, the CLR takes care of memory management. Is there any need for a
programmer to explicitly release memory and resources? If yes, why and how?
If the application is using expensive external resource, it is recommend to explicitly release the
resource before the garbage collector runs and frees the object. We can do this by implementing
the Dispose method from the IDisposable interface that performs the necessary cleanup for the
object. This can considerably improve the performance of the application.
When do we generally use destructors to release resources?
If the application uses unmanaged resources such as windows, files, and network connections, we
use destructors to release resources.

What is a constructor in C#?


Constructor is a class method that is executed when an object of a class is created. Constructor
has the same name as the class, and usually used to initialize the data members of the new
object.
We cannot create instances of static classes. Can we have constructors for static
classes?
Yes, static classes can also have constructors.

Can you prevent a class from being instantiated?


Yes, a class can be prevented from being instantiated by using a private constructor
Can a class have static constructor?
Yes, a class can have static constructor. Static constructors are called automatically, immediately
before any static fields are accessed, and are generally used to initialize static class members. It is
called automatically before the first instance is created or any static members are referenced.
Static constructors are called before instance constructors

Can you mark static constructor with access modifiers?


No, we cannot use access modifiers on static constructor.

Can you have parameters for static constructors?


No, static constructors cannot have parameters.

What happens if a static constructor throws an exception?


If a static constructor throws an exception, the runtime will not invoke it a second time, and the
type will remain uninitialized for the lifetime of the application domain in which your program is
running.

Give 2 scenarios where static constructors can be used?


1. A typical use of static constructors is when the class is using a log file and the constructor is
used to write entries to this file.
2. Static constructors are also useful when creating wrapper classes for unmanaged code, when
the constructor can call the LoadLibrary method.

What are the 4 pillars of any object oriented programming language?


1. Abstraction
2. Inheritance
3. Encapsulation
4. Polymorphism
Does C# support multiple class inheritance?
No, C# supports single class inheritance only. However classes can implement multiple
interfaces at the same time.

What are Access Modifiers in C#?


In C# there are 5 different types of Access Modifiers.
Public
The public type or member can be accessed by any other code in the same assembly or
another assembly that references it.

Private
The type or member can only be accessed by code in the same class or struct.

Protected
The type or member can only be accessed by code in the same class or struct, or in a
derived class.

Internal
The type or member can be accessed by any code in the same assembly, but not from
another assembly.

Protected Internal
The type or member can be accessed by any code in the same assembly, or by any
derived class in another assembly.

2.What are the different types of assemblies – name them?


Private
Public/Shared
Satellite assembly
3.What is GAC? What are the steps to be taken to pick up the latest version from GAC?
This Global Assembly Cache(GAC) stores .NET assemblies to be shared by several applications on
that computer.publisher policy file is the configuration file to redirect to different version
1. Create the publisher Policy assembly using the assembly linker
2. Add the publisher policy assembly to the GAC using GACutil tool
Gacutil /i
3. During runtime CLR is looking into the publisher policy file and redirect the application to bind with
new version assembly as specified inside the publisher policy.

4.How do we use different versions of private assemblies in same application without re-build?
In Asseblyinfo file need specify assembly version.
assembly: AssemblyVersion - See more at: http://www.aired.in/2009/10/c-realtime-interview-questions-
and.html#sthash.KPLMJ5oB.dpuf

You might also like