Skip to main content

CSharp C# .NET Interview Questions and Answers

** C# .NET **
*    How can you copy a class?
   - A shallow copy copies all value types and reference values - references will still point to the same place in memory  myClass.MemberwiseClone();
   - A deep copy copies all fields and referenced objects to new memory, need to create a new

*    What is boxing?
   - Casting a value type as an object (ref type) type to allow it to be garbarge collected. Has an overhead and should be avoided.
      int a; object b = (obj)a;

*    Explain the garbage collecting with reference to generations
   - Generations exist to increase GC performance, Gen 0 objects are new, have never been observed by the GC, once the GC has run it compacts objects that are not collected and makes them Gen 1, on the next collection gen 1 is moved to gen 2 if its not collected. Gen 0 is collected more often since newer objects have shorter lives.

*    What would you use to store 1mil integer values?
   - array for small size, O(n) access
   - ArrayList is dynamic and has better access
   - best is List<t> (generic list) to save boxing/unboxing

*    How do you access a control from a worker thread?
   - Use a control.Invoke method (and usually an InvokeRequired to check if its required)

*    What is the difference between control.begininvoke/invoke?
   - .beginInvoke will run asynchronously
   - .Invoke will wait for the thread to complete before the calling thread continues

*    How do you think this is implemented 'under the hood'?
   - Control.InvokeRequired() to work out which is best

*    What is a delegate?
   - A type safe function pointer, a way of passing the code to run upon an event occurance. It encapulates a method as an object so it can be

passed.

*    What are the two different types of delegate?
   - Multicast or single. Multicast calls a number of methods in turn.

*    How is this different to calling begininvoke on a delegate?
   - BeginInvoke calls the delegate on a thread in the thread pool and returns to the calling thread imediately

*    How else could you do this?
   - Create your own thread explicitly and call the delegate

*    What are the methods on the Object class
   - Equals, ToString, GetHashCode, GetType, protected MemberWiseClone (makes a shallow copy - copies value type fields and references)

*    Why would you override any of the above methods?
   - Extend functionality / Provide custom functionality

*    Explain the Singleton pattern briefly
   - Can only ever be one instance, has Private constructor, public GetInstance method creates instance if there isnt on and returns instance reference.

*    If I needed to call a Win32 function, how would I do it?
   - P/invoke (platform invoke) or MC++ IJW. Be aware that is it unmanaged so needs manual garbage collection and error handling.

*    I want to create a custom control. What are my options?
   - Subclass control or an existing control and override the OnPaint method
     which passes you the rect and graphics surface to draw to,
     graphics.drawline etc

*    How do I draw outside of an OnPaint method
   - call control.CreateGraphics

*    What does the 'using' keyword do for you?
   - As a directive is means full namespaces dont have to be specified
      eg. using System
   - as a statement it automatically calls dispose when the object goes out of scope
     (assuming object is implements IDisposible)
     
      eg.
      using (System.IO.StreamReader reader = new StreamReader("readme.txt"))
      {
           // read from the file
      }

      // readme.txt has now been closed automatically.


*    Explain what goes on in the garbage collector
   - GC stops all threads, marks and sweeps a generation from roots, compacts heap, puts
   finalizable objects on the finalise queue to be run, extra points for
   saying heap compaction results in fast allocs, and that position in the
   heap implies age/generation, and that un-needed finalisers result in more
   load on the GCollector process

*    How would you ensure something is cleared by the Garbage collector?
   - Implement IDisposable

*    How would I implement IDisposable?
     Why would I implement it?
        - Create a dispose(bool) and call dispose(true) from
   dispose method and dispose(false) from finaliser, set disposed flag,
   throw ObjectDisposedException if flag set on any subsequent call, call
   SupressFinalize in dispose call.
   Implemented to free up expensive resources

*    Can you call finalize on an object directly?
   - No, you can call GC.Collect or object.Dispose which may call the finalizer

*    How do I pass a value type as a reference?
   - use the 'ref' keyword eg void myfunc(ref int param)

*    Are structs ref or val type, how are they passed?
   - Value type (classes are referance types) to pass memory is copied onto the stack,

*    How about strings?  
   - Strings are immutable ref types

*    Whats the diff between
      MyClass A = (MyClass)obj;    and
      MyClass A = obj as MyClass;
   - static cast throws InvalidCastException and 'as' returns null if the cast is invalid

*    Whats new in .NET 3.5?
   - WCF/WPF/Linq/Lamda expressions/Var type/Object Initialisers/Extension Methods/Automatic Properties

*    How would you go about beginning a project? What tools would you use? Do you just start coding?
   - NUnit testing, UML, case diagrams, peer review, iterative test solutions etc, general chat

*    What is abstract vs interface?
   - Abstract class is a class but cannot be instantiated, it forces subclasses to have certain elements
     It may have some or no implementation and contains at least one abstract item which must be implemented by the subclass.
     You can only inherit from one abstract class
   - Interface is not a class, it has no implementation, you can implement multiple interfaces. All members are virtual and MUST be overridden by the implementing class.


*    What is overload vs override?
   - Overloading is the same method name with different parameters. Different Signature, maybe similar implementation.
   - Overriding is where a subclass implements the same method signature that is specified in its parent. Same signature, different implementation.


*    How can you hide the base implementation of a method?
*    What is the new keyword in overridden methods?
   - The new keyword in an overridden method if the base class isnt virtual. This will hide the base implementation.

*    What is an extension method?
   - Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type.

*    How can you write an extension method?
*    What is the this keyword?
   - Using the this keyword before the first parameter or a method indicates a static method is an extension method.
      eg   public static int WordCount(this String str) ...

*    Have you ever worked in a team that spanned more than one location?
     What issues came up and how did you solve them?
    - Basically can he handle ignorant oafs in a different time zone,
      is he diplomatic, and gives him a chance to chat a bit so I can get an idea on personality

*    Abstract class vs Interface
   - Abstract class cannot be instantiated and has one of more abstract methods but may have some implemented methods
     this seperates the base functionality from the variable. Abstract classes define core functions of a class.
   - Interface has no implemented methods and defines a template for how a class using the interface should be constructed

*    Set vs Collection
   - A set is a collection that contains no duplicate entries. This isnt included in System.Collections, can use HashSet<T> in .NET 3.5

*    Hashtable vs Dictionary?
   - Hashtable stores values in buckets with buckets having a hashcode based on the object key. Object in a Hashtable must implement GetHashCode.
   - Dictionary are the same as hashtable but are faster for value types because there is no boxing.

*    Try/Catch and Finally
   - Try catch allows you to attempt to use a resource and catch any exceptions. Finally is normally used to release the resource after the try block

*    What does the keyword Volitile signify in C#?
   - A property marked volitile may be changed by an outside entity. Often used for system values that are changing.

*    What is the difference between a value type and a reference type?
   - Value types are stored on the stack, reference types are stored on the heap with a reference from the stack.

*    What is int, value or reference?
   - Value, stored on the stack, passed by value

*    What is string, value or reference?
   - Reference, stored on the heap. Also immutable (cannot be changed)

*    Why should you not write a loop that has a core that adds bits to a string like this stringVar1+="stuff";
   - Strings are immutable so adding or modifing a string value actually creates a whole new string in memory then copying it over. This is relatively very slow. 

*    What class should you use for that sort of thing?
   - Should use StringBuilder, strings are only immutable to public interface, string builder only copies the added part of the string so is much faster. 

*    ADO.NET
   - A collection of OO Libraries to allow you to interact with data sources.

*    Can you make a derived method static if the parent is not static?
   - No

*    What is the difference between a DataSet and a DataReader?
   - Dataset is a colelction of DataTables. DataReader provides a stream of data from the database.

*    What does the access moddifier PROTECTED INTERNAL meant?
   - It means the object is protected (available from derived classed) OR internal (available from the same assembly)

*    What is managed code vs unmanged code?
   - Managed code runs in a runtime (CLR) The runtime handles garbage collection and exception handling.
     Unmanaged code talks to hardware, nothing is handled automatically. Good place to look for memory leaks.

*    Event vs Delegate
   - Interface cannot have a delegate but can have an event declaration
   - Events can only be raised by the class that declares an event, unlike delegate.

*    Exceptions in a multicast delegate or event?
   - If an exception is thrown in a multicast delegate or event the rest of the methods after the erroring one will not execute
     To overcome this use foreach(EventHandler handler in _myEvent.GetInvocationList()) and try catch each one individually
     Thread safety: you can lock on the (this) (current instance) when you take a copy of the delegate then iterate over the copy

*    What is CAS in .NET?
   - Code Access Security - the CAS policy is used by the CLR to maintain security.

*    What is are Func and Action? What is the difference?
   - Func and Action are predefined delegates with generic pararmeters. Func has an out parameter, Action does not
     Funcs used frequently with lambda     Func<int, int> sqr = x => x * x;

Comments

Popular posts from this blog

anchor suma photo gallery,suma photos

priyamani hot and sexy photos , Priyamani wallpapers

 Priyamani hot and sexy photos  Priyamani Images

actress anushka shetty childhood baby photos,stunning beautiful telugu actress anushka still,anushka shetty in silk saree stills ,actress anushka at bahubali movie making photos,Anushka Hot Pics, Hottest Anushka Shetty Sexy Stills