1) Single inheritance means that a C# class can inherit from only one base class.
2) C# enables you to group your classes into a collection of classes called a namespace.
Namespaces have names, and can help organize collections of classes into logical groupings.
3) C# lets you work with two types of data: value types and reference types. Value types hold
actual values. Reference types hold references to values stored elsewhere in memory.
4) Primitive types such as char, int and float, as well as enumerated values and structures, are
value types. Reference types hold variables that deal with objects and arrays
5) You can work with both one-dimensional and multidimensional arrays in C#.
Multidimensional arrays can be rectangular, in which each of the arrays has the same
dimensions, or jagged, in which each of the arrays has different dimensions.
6) Functions can have four kinds of parameters:
• Input parameters have values that are sent into the function, but the function cannot
change those values.
• Output parameters have no value when they are sent into the function, but the function
can give them a value and send the value back to the caller.
• Reference parameters pass in a reference to another value. They have a value coming
in to the function, and that value can be changed inside the function.
• Params parameters define a variable number of arguments in a list.
7) Enum type declarations specify a type name
for a related group of constants, Using the enum names in code makes code more readable
8) C# provides a built-in mechanism for defining and handling events. If you write a class that
performs a lengthy operation, you may want to invoke an event when the operation is
completed. Clients can subscribe to that event and catch the event in their code, which enables
them to be notified when you have completed your lengthy operation. The event handling
mechanism in C# uses delegates, which are variables that reference a function.
9) You can write a piece of code called an indexer to enable your class to be accessed
as if it were an array. Suppose you write a class called Rainbow, for example, that contains a
set of the colors in the rainbow. Callers may want to write MyRainbow[0] to retrieve the first
color in the rainbow. You can write an indexer into your Rainbow class to define what should
be returned when the caller accesses your class, as if it were an array of values.
10) Attributes declare additional information about your class to the CLR. Attributes
enables you, the developer, to bind information to classes. Attributes can also be used to bind runtime information to a class
11) The code that is output by the C# compiler is written in a language called Microsoft
Intermediate Language, or MSIL
12) All .NET-compatible languages, including Visual Basic .NET and Managed C++,
produce MSIL when their source code is compiled. Because all of the .NET languages
compile to the same MSIL instruction set,
13) MSIL code is turned into
CPU-specific code when the code is run for the first time. This process is called "just-in-time"
compilation, or JIT. The job of a JIT compiler is to translate your generic MSIL code into
machine code that can be executed by your CPU.
14) The compilation process also outputs metadata, Think of metadata as a "table of contents" for your compiled code. The C# compiler places
metadata in the compiled code along with the generated MSIL. This metadata accurately
describes all the classes you wrote and how they are structured.
15) An assembly is a package of code and metadata. When you deploy a set of classes in an
assembly, you are deploying the classes as a unit; and those classes share the same level of
version control, security information, and activation requirements. Think of an assembly as a
"logical DLL."
16) There are two types of assemblies: private assemblies and global assemblies. When you build
your assembly, you don't need to specify whether you want to build a private or a global
assembly. The difference is apparent when you deploy your assembly. With a private
assembly, you make your code available to a single application.
17) The .NET Framework contains a list of global assemblies in a facility
called the global assembly cache, and the .NET Microsoft Framework SDK includes utilities
to both install and remove assemblies from the global assembly cache. GAC
18) Every application in C# must have a method called Main().
19) public static void Main()
By declaring your Main() method as public, you are
creating an entry point for Windows to start the application when a user wants to run it.
The word Static in the method declaration means that the compiler should allow only one
copy of the method to exist in memory at any given time. Because the Main() method is the
entry point into your application, it would be catastrophic to allow the entry point to be loaded
more than once
Void means that your application returns no value after it has
completed. This sample application isn't very advanced, so no return value is needed; under
normal circumstances, however, the Main() function would typically return an integer value
by replacing the word void with int.
20) The C# compiler does not ordinarily allow you to use any of the reserved keywords as an
identifier name. You'll get an error if, for example, you try to name a class static. If you really
need to use a keyword name as an identifier, however, you can precede the identifier with the
@ symbol. This overrides the compiler error and enables you to use a keyword as an
identifier.
21) The C# compiler does, in fact, accept any
of four possible constructs for the Main() function:
• public static void Main()
• public static void Main(string[] Arguments)
• public static int Main()
• public static int Main(string [] Arguments)
The second form, public static void Main(string[] Arguments), does not return a value to the
caller. It does, however, take in an array of strings. Each string in the array corresponds to a
command-line argument supplied when the program executes.
22) C# makes a distinction between assigned and unassigned variables. Assigned variables are
given a value at some point in the code, and unassigned variables are not given a value in the
code. Working with unassigned variables is forbidden in C#, because their values are not
known and using the variables can lead to errors in your code. HOWEVER In some cases, C# gives default values to variables. A variable declared at the class level is
one such case.
23) C# enables you to group variables into structures. By defining a structure for your data, the
entire group can be managed under a single structure name, regardless of the number of
variables that the structure contains. The entire set of variables can be easily manipulated by
working with a single structure, rather than having to keep track of each individual variable
separately
24) Structures in C# are value types, not reference types. This means that structure variables
contain the structure's values directly, rather than maintaining a reference to a structure found
elsewhere in memory.
25) Structures may also be made up of variables of different types.
struct Employee
{
public string FirstName;
public string LastName;
public string Address;
public string City;
public string State;
public ushort ZIPCode;
public decimal Salary;
}
As with all statements in C#, you can declare a structure only from within a class.
you can then use it like:
Employee emp;
emp.Firstname
26) C# does not allow you to initialize structure
members when they are declared. Take a look at the error in the following code:
struct Point
{
public int X = 100;
public int Y = 200;
}
This declaration produces errors from the compiler:
error CS0573: 'MyClass.Point.X': cannot have instance field
initializers in structs
error CS0573: 'MyClass.Point.Y': cannot have instance field
initializers in structs
You can use a special method called a constructor to initialize structure members to nonzero
values.
27) If you want to invoke a constructor on a structure, you must use the new keyword, followed by the
name of the structure,
Point MyThirdPoint = new Point(250, 475);
This statement says: "Create a new Point structure using the constructor with two integers.
Assign its value to the MyThirdPoint variable.
28) The parameterless constructor syntax is also shown in Listing 7-3:
Point MyFirstPoint = new Point();
This tells the C# compiler that you want to initialize the structure using its default behavior.
29) You may also write methods in your structures. These methods follow the same rules as class
methods:
30) Properties within a structure enable you to read, write, and compute values with the use of
accessors. Unlike fields, properties are not considered variables; therefore, they do not
designate storage space. Because a property does not allocate storage space, it cannot be
passed as a ref or out parameter.
31) Interfaces are a way of ensuring that someone using your class has abided by all the rules you
set forth to do so. This can include implementing certain methods, properties, and events.
When you expose an interface, users of your interface must inherit that interface; and in doing
so, they are bound to create certain methods and so forth. This ensures that your class and/or
structure is used as it was intended.
You can include an interface within a structure as well.
Collection and sharing of, interview questions and answers asked in various interviews, faqs and articles.....
Showing posts with label C# Interview questions. Show all posts
Showing posts with label C# Interview questions. Show all posts
what is the difference between Convert.toString and .toString ()?
Just to give an understanding of what the above question means see the below code.
int i =0;
MessageBox.Show(i.ToString());
MessageBox.Show(Convert.ToString(i));
We can convert the integer “i” using “i.ToString()” or “Convert.ToString” so what is the
difference. The basic difference between them is “Convert” function handles NULLS while
“i.ToString()” does not it will throw a NULL reference exception error. So as a good coding
practice using “convert” is always safe.
int i =0;
MessageBox.Show(i.ToString());
MessageBox.Show(Convert.ToString(i));
We can convert the integer “i” using “i.ToString()” or “Convert.ToString” so what is the
difference. The basic difference between them is “Convert” function handles NULLS while
“i.ToString()” does not it will throw a NULL reference exception error. So as a good coding
practice using “convert” is always safe.
How to prevent my .NET DLL to be decompiled?
By design, .NET embeds rich Meta data inside the executable code using MSIL. Any one can
easily decompile your DLL back using tools like ILDASM (owned by Microsoft) or Reflector for
.NET which is a third party. Secondly, there are many third party tools, which make this
decompiling process a click away. So any one can easily look in to your assemblies and reverse
engineer them back in to actual source code and understand some real good logic, which can
make it easy to crack your application.
The process by which you can stop this reverse engineering is using “obfuscation”. It is a
technique, which will foil the decompilers. Many third parties (XenoCode, Demeanor for .NET)
provide .NET obfuscation solution. Microsoft includes one that is Dotfuscator Community
Edition with Visual Studio.NET.
easily decompile your DLL back using tools like ILDASM (owned by Microsoft) or Reflector for
.NET which is a third party. Secondly, there are many third party tools, which make this
decompiling process a click away. So any one can easily look in to your assemblies and reverse
engineer them back in to actual source code and understand some real good logic, which can
make it easy to crack your application.
The process by which you can stop this reverse engineering is using “obfuscation”. It is a
technique, which will foil the decompilers. Many third parties (XenoCode, Demeanor for .NET)
provide .NET obfuscation solution. Microsoft includes one that is Dotfuscator Community
Edition with Visual Studio.NET.
What is CODE Access security?
CAS is part of .NET security model that determines whether a piece of code is allowed to run and
what resources it can use while running. Example CAS will allow an application to read but not
to write and delete a file or a resource from a folder..
what resources it can use while running. Example CAS will allow an application to read but not
to write and delete a file or a resource from a folder..
what is the difference between System exceptions and Application
All exception derives from Exception Base class. Exceptions can be generated programmatically
or can be generated by system. Application Exception serves as the base class for all applicationspecific
exception classes. It derives from Exception but does not provide any extended
functionality. You should derive your custom application exceptions from Application Exception.
Application exception is used when we want to define user-defined exception, while system
exception is all that is defined by .NET.
or can be generated by system. Application Exception serves as the base class for all applicationspecific
exception classes. It derives from Exception but does not provide any extended
functionality. You should derive your custom application exceptions from Application Exception.
Application exception is used when we want to define user-defined exception, while system
exception is all that is defined by .NET.
What is the difference between VB.NET and C#?
Well this is the most debatable issue in .NET community and people treat languages like religion.
It is a subjective matter which language is best. Some like VB.NET’s natural style and some like
professional and terse C# syntaxes. Both use the same framework and speed is very much
equivalents. Still let us list down some major differences between them:-
Advantages VB.NET:-
• Has support for optional parameters that makes COM interoperability much easy.
• With Option Strict off late binding is supported.Legacy VB functionalities can be
used by using Microsoft.VisualBasic namespace.
• Has the WITH construct which is not in C#.
• The VB.NET parts of Visual Studio .NET compiles your code in the background.
While this is considered an advantage for small projects, people creating very
large projects have found that the IDE slows down considerably as the project
gets larger.
Advantages of C#
• XML documentation is generated from source code but this is now been
incorporated in Whidbey.
• Operator overloading which is not in current VB.NET but is been introduced in
Whidbey.
• Use of this statement makes unmanaged resource disposal simple.
• Access to Unsafe code. This allows pointer arithmetic etc, and can improve
performance in some situations. However, it is not to be used lightly, as a lot of
the normal safety of C# is lost (as the name implies).This is the major difference
that you can access unmanaged code in C# and not in VB.NET.
It is a subjective matter which language is best. Some like VB.NET’s natural style and some like
professional and terse C# syntaxes. Both use the same framework and speed is very much
equivalents. Still let us list down some major differences between them:-
Advantages VB.NET:-
• Has support for optional parameters that makes COM interoperability much easy.
• With Option Strict off late binding is supported.Legacy VB functionalities can be
used by using Microsoft.VisualBasic namespace.
• Has the WITH construct which is not in C#.
• The VB.NET parts of Visual Studio .NET compiles your code in the background.
While this is considered an advantage for small projects, people creating very
large projects have found that the IDE slows down considerably as the project
gets larger.
Advantages of C#
• XML documentation is generated from source code but this is now been
incorporated in Whidbey.
• Operator overloading which is not in current VB.NET but is been introduced in
Whidbey.
• Use of this statement makes unmanaged resource disposal simple.
• Access to Unsafe code. This allows pointer arithmetic etc, and can improve
performance in some situations. However, it is not to be used lightly, as a lot of
the normal safety of C# is lost (as the name implies).This is the major difference
that you can access unmanaged code in C# and not in VB.NET.
What is concept of Boxing and Unboxing ?
Boxing and unboxing act like bridges between value type and reference types. When we convert
value type to a reference type it’s termed as boxing. Unboxing is just vice-versa. When an object
box is cast back to its original value type, the value is copied out of the box and into the
appropriate storage location.
Below is sample code of boxing and unboxing where integer data type are converted in to object
and then vice versa.
int i = 1;
object obj = i; // boxing
int j = (int) obj; // unboxing
value type to a reference type it’s termed as boxing. Unboxing is just vice-versa. When an object
box is cast back to its original value type, the value is copied out of the box and into the
appropriate storage location.
Below is sample code of boxing and unboxing where integer data type are converted in to object
and then vice versa.
int i = 1;
object obj = i; // boxing
int j = (int) obj; // unboxing
What are Value types and Reference types?
Value types directly contain their data that are either allocated on the stack or allocated in-line in
a structure. So value types are actual data.
Reference types store a reference to the value's memory address, and are allocated on the heap.
Reference types can be self-describing types, pointer types, or interface types. You can view
reference type as pointers to actual data.
Variables that are value types each have their own copy of the data, and therefore operations on
one variable do not affect other variables. Variables that are reference types can refer to the same
object; therefore, operations on one variable can affect the same object referred to by another
variable. All types derive from the System. Object base type.
(A) What are different types of JIT?
JIT compiler is a part of the runtime execution environment.
In Microsoft .NET there are three types of JIT compilers:
• Pre-JIT: - Pre-JIT compiles complete source code into native code in a single
compilation cycle. This is done at the time of deployment of the application.
• Econo-JIT: - Econo-JIT compiles only those methods that are called at runtime.
However, these compiled methods are removed when they are not required.
• Normal-JIT: - Normal-JIT compiles only those methods that are called at runtime.
These methods are compiled the first time they are called, and then they are stored in
cache. When the same methods are called again, the compiled code from cache is used
for execution.
What is reflection?
All .NET assemblies have metadata information stored about the types defined in modules. This
metadata information can be accessed by mechanism called as “Reflection”. System. Reflection
can be used to browse through the metadata information.
Using reflection, you can also dynamically invoke methods using System.Type.Invokemember.
Below is sample source code if needed you can also get this code from CD provided, go to
“Source code” folder in “Reflection Sample” folder.
Public Class Form1
Private Sub Form1_Load (ByVal sender As System. Object, ByVal e as
System.EventArgs) Handles MyBase.Load
Dim Pobjtype As Type
Dim PobjObject As Object
Dim PobjButtons As New Windows.Forms.Button ()
Pobjtype = PobjButtons.GetType ()
For Each PobjObject in Pobjtype.GetMembers
LstDisplay.Items.Add (PobjObject.ToString ())
Next
End Sub
End Class
Most common C# Interview questions
1. What’s the advantage of using System.Text.StringBuilder over System.String? Can you store multiple data types in System.Array? No.
2. What’s the difference between the System.Array.CopyTo() and System.Array.Clone()?
3. How can you sort the elements of the array in descending order?
4. What’s the .NET datatype that allows the retrieval of data by a unique key?
5. What’s class SortedList underneath?
6. Will finally block get executed if the exception had not occurred?
7. What’s the C# equivalent of C++ catch (…), which was a catch-all statement for any possible exception?
8. Can multiple catch blocks be executed?
9. Why is it a bad idea to throw your own exceptions?
10. What’s a delegate?
11. What’s a multicast delegate?
12. How’s the DLL Hell problem solved in .NET?
13. What are the ways to deploy an assembly?
14. What’s a satellite assembly?
15. What namespaces are necessary to create a localized application?
16. What’s the difference between // comments, /* */ comments and /// comments?
17. How do you generate documentation from the C# file commented properly with a command-line compiler?
18. What’s the difference between and
2. What’s the difference between the System.Array.CopyTo() and System.Array.Clone()?
3. How can you sort the elements of the array in descending order?
4. What’s the .NET datatype that allows the retrieval of data by a unique key?
5. What’s class SortedList underneath?
6. Will finally block get executed if the exception had not occurred?
7. What’s the C# equivalent of C++ catch (…), which was a catch-all statement for any possible exception?
8. Can multiple catch blocks be executed?
9. Why is it a bad idea to throw your own exceptions?
10. What’s a delegate?
11. What’s a multicast delegate?
12. How’s the DLL Hell problem solved in .NET?
13. What are the ways to deploy an assembly?
14. What’s a satellite assembly?
15. What namespaces are necessary to create a localized application?
16. What’s the difference between // comments, /* */ comments and /// comments?
17. How do you generate documentation from the C# file commented properly with a command-line compiler?
18. What’s the difference between
XML documentation tag?
19. Is XML case-sensitive?
20. What debugging tools come with the .NET SDK?
21. What does the This window show in the debugger?
22. What does assert() do?
23. What’s the difference between the Debug class and Trace class?
24. Why are there five tracing levels in System.Diagnostics.TraceSwitcher?
25. Where is the output of TextWriterTraceListener redirected?
Subscribe to:
Posts (Atom)