Documents
Documents
Documents
[DllImport("user32.dll")]
public static extern int MessageBoxA(int h, string m, string c, int type);
public static int Main()
{
return MessageBoxA(0, "Hello World!", "Caption", 0);
}
}
This example shows the minimum requirements for declaring a C# method that is
implemented in a native DLL. The method C.MessageBoxA() is declared with the
static and external modifiers, and has the DllImport attribute, which tells the
compiler that the implementation comes from the user32.dll, using the default
name of MessageBoxA. For more information, look at the Platform Invoke tutorial
in the documentation.
What is the difference between a struct and a class in C#?
From language spec:
The list of similarities between classes and structs is as follows. Longstructs can
implement interfaces and can have the same kinds of members as classes.
Structs differ from classes in several important ways; however, structs are value
types rather than reference types, and inheritance is not supported for structs.
Struct values are stored on the stack or in-line. Careful programmers can
sometimes enhance performance through judicious use of structs. For example,
the use of a struct rather than a class for a Point can make a large difference in
the number of memory allocations performed at runtime. The program below
creates and initializes an array of 100 points. With Point implemented as a class,
101 separate objects are instantiated-one for the array and one each for the 100
elements.
What is the difference between the Debug class and Trace class?
Documentation looks the same. Use Debug class for debug builds, use Trace
class for both debug and release builds.
How can you overload a method?
Different parameter data types, different number of parameters, different order of
parameters.
What debugging tools come with the .NET SDK?
CorDBG - command-line debugger, and DbgCLR - graphic debugger. Visual
Studio .NET uses the DbgCLR. To use CorDbg, you must compile the original C#
file using the /debug switch.
What does Dispose method do with the connection object?
Deletes it from the memory.
{
public static void Main()
{
int x = 3;
switch(x)
{
case 0:
// do something
goto case 1;
case 1:
// do something in common with 0
goto default;
default:
// do something in common with 0, 1, and anything else
break;
}
}
}
How can I access the registry from C# code?
By using the Registry and RegistryKey classes in Microsoft.Win32, you can
easily access the registry. The following is a sample that reads a key and
displays its value:
using System;using Microsoft.Win32;
class regTest
{
public static void Main(String[] args)
{
RegistryKey regKey;
Object value;
regKey = Registry.LocalMachine;
regKey =
regKey.OpenSubKey("HARDWAREDESCRIPTIONSystemCentralProcessor ");
value = regKey.GetValue("VendorIdentifier");
Console.WriteLine("The central processor of this machine is: {0}.", value);
}
}
How is the DLL Hell problem solved in .NET?
Assembly versioning allows the application to specify not only the library it needs
to run (which was available under Win32), but also the version of the assembly
What is a delegate?
A delegate object encapsulates a reference to a method. In C++ they were
referred to as function pointers.
that the references in the original Array point to. In contrast, a deep copy of an
Array copies the elements and everything directly or indirectly referenced by the
elements.
How can you sort the elements of the array in descending order?
By calling Sort() and then Reverse() methods.
Whats the .NET collection class that allows an element to be accessed
using a unique key?
HashTable.
What class is underneath the SortedList class?
A sorted HashTable.
Will the finally block get executed if an exception has not occurred?
Yes.
Whats the C# syntax to catch any possible exception?
A catch block that catches the exception of type System.Exception. You can also
omit the parameter data type in this case and just write catch {}.
Can multiple catch blocks be executed for a single try statement?
No. Once the proper catch block processed, control is transferred to the finally
block (if there are any).
Explain the three services model commonly know as a three-tier
application.
Presentation (UI), Business (logic and underlying code) and Data (from storage
or other sources).
What is the syntax to inherit from a class in C#?
Place a colon and then the name of the base class.
Example: class MyNewClass : MyBaseClass
Can you prevent your class from being inherited by another class?
Yes. The keyword sealed will prevent the class from being inherited.
Can you allow a class to be inherited, but prevent the method from being
over-ridden?
Yes. Just leave the class public and make the method sealed.
Whats an abstract class?
A class that cannot be instantiated. An abstract class is a class that must be
inherited and have the methods overridden. An abstract class is essentially a
blueprint for a class without any implementation.
No. The signature of the virtual method must remain the same. (Note: Only the
keyword virtual is changed to keyword override)
What are the different ways a method can be overloaded?
Different parameter data types, different number of parameters, different order of
parameters.
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?
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.
Can you change the value of a variable while debugging a C# application?
Yes. If you are debugging via Visual Studio.NET, just go to Immediate window.
What is the role of the DataReader class in ADO.NET connections?
It returns a read-only, forward-only rowset from the data source. A DataReader
provides fast access when a forward-only sequential read is needed.What are
advantages and disadvantages of Microsoft-provided data provider classes in
ADO.NET?
SQLServer.NET data provider is high-speed and robust, but requires SQL Server
license purchased from Microsoft. OLE-DB.NET is universal for accessing other
sources, like Oracle, DB2, Microsoft Access and Informix. OLE-DB.NET is a
.NET layer on top of the OLE layer, so its not as fastest and efficient as
SqlServer.NET.
What is the wildcard character in SQL?
Lets say you want to query database with LIKE for all employees whose name
starts with La. The wildcard character is %, the proper query with LIKE would
involve La%.
When should you call the garbage collector in .NET?
As a good rule, you should not call the garbage collector. However, you could call
the garbage collector when you are done using a large object (or set of objects)
to force the garbage collector to dispose of those very large objects from
memory. However, this is usually not a good practice.
How do you convert a value-type to a reference-type?
Use Boxing.
Explain manifest & metadata.
Answer1
Manifest is metadata about assemblies. Metadata is machine-readable
There is a perfect tool provide by .net frameworks calls Garbage collector, where
by mean of GC we can clean up the object and reclaim the memory. The
namespace used is System.GC
Answer2
the run time will maintain a service called as garbage collector. This service will
take care of deallocating memory corresponding to objects. it works as a thread
with least priority. when application demands for memory the runtime will take
care of setting the high priority for the garbage collector, so that it will be called
for execution and memory will be released. the programmer can make a call to
garbage collector by using GC class in system name space.
How can you clean up objects holding resources from within the code?
Call the dispose method from code for clean up of objects
How can you clean up objects holding resources from within the code?
Call the dispose method from code for clean up of objects
Which controls do not have events?
Timer control.
What is the maximum size of the textbox?
65536.
Which property of the textbox cannot be changed at runtime?
Locked Property.
Which control cannot be placed in MDI?
The controls that do not have events.
What is the difference between proc. sent BY VAL and BY SUB?
BY VAL: changes will not be reflected back to the variable.
By REF: changes will be reflected back to that variable.( same as & symbol in c,
c++)
Both In Try block and In Finally block will be displayed. Whether the return is in
the try block or after the try-finally block, performance is not affected either way.
The compiler treats it as if the return were outside the try block anyway. If its a
return without an expression (as it is above), the IL emitted is identical whether
the return is inside or outside of the try. If the return has an expression, theres an
extra store/load of the value of the expression (since it has to be computed within
the try block).
I was trying to use an out int parameter in one of my functions. How should
I declare the variable that I am passing to it?
You should declare the variable as an int, but when you pass it in you must
specify it as out, like the following: int i; foo(out i); where foo is declared as
follows:
[return-type] foo(out int o) { }
How do you specify a custom attribute for the entire assembly (rather than
for a class)?
Global attributes must appear after any top-level using clauses and before the
first type or namespace declarations. An example of this is as follows:
using System;
[assembly : MyAttributeClass] class X {}
Note that in an IDE-created project, by convention, these attributes are placed in
AssemblyInfo.cs.