Showing posts with label c#. Show all posts
Showing posts with label c#. Show all posts

Sunday, July 1, 2012

Why is an Object Pool required

Why is an Object Pool required?


To enhance performance and reduce the load of creating new objects, instead re using existing objects stored in memory pool. 
Object Pool is a container of objects that are for use and have already been created. Whenever an object creation request occurs, the pool manager serves the request by allocating an object from the pool. This minimizes the memory consumption and system's resources by recycling and re-using objects. 
When the task of an object is done, it is sent to the pool rather than being destroyed. This reduces the work for garbage collector and fewer memory allocations occur.

GetCommandLineArgs() method in C# .NET


What is the use of GetCommandLineArgs() method in C#.NET?


With GetCommandLineArgs() method, the command line arguments can be accessed. 
The value returned is an array of strings.

Compare C# .NET Generics and C++ Templates


How does C#.NET Generics and C++ Templates compare?


C# generics and templates in C++ are more or less similar syntactically.


C# Generic types are strong typed. C++ Templates are loosely typed.
C# Generic types are instantiated at the runtime. C++ templates are instantiated at the compile time.
C# Generic types do not permit the type parameters to have default values. C++ templates do.

Achieve polymorphism in C# .NET


How to achieve polymorphism in C#.NET?


Polymorphism is when a class can be used as more than one type through inheritance. It can be used as its own type, any base types, or any interface type if it implements interfaces.It can be achieved in the following ways.


Derived class inherits from a base class and it gains all the methods, fields, properties and events of the base class.


To completely take over a class member from a base class, the base class has to declare that member as virtual.

Use of System.Environment class in C# .NET


What is the use of System.Environment class in C#.NET?


The System.Environment class can be used to retrieve information like below :

  • Command-line arguments.
  • The exit code.
  • Environment variable settings.
  • Contents of the call stack.
  • Time since last system boot.
  • The version of the common language runtime.


Prevent class from being inherited in C#.NET


How to prevent a class from being inherited in C#.NET?


The sealed modifier is used to prevent derivation from a class. An error occurs if a sealed class is specified as the base class of another class. A sealed class cannot also be an abstract class.


Add ReadOnly property in C#.NET?


How to add a ReadOnly property in C#.NET?


Properties can be made read-only by having only a get accessor in the implementation. For example


public class X
{
          public X(int id)
          {
               x_id = id;
          }
          public int ID
          {
              get
              {
                   return x_id;
              }
          }
}

Implementation inheritance and interface inheritance

What are implementation inheritance and interface inheritance?


Implementation inheritance is achieved when a class is derived from another class in such a way that it inherits all its members.


Interface inheritance is when a class inherits only the signatures of the functions from another class.

C# interview questions with answers, Part 3


C# developer interview questions and answers.


What is the difference between Finalize() and Dispose()?


Dispose() is called by as an indication for an object to release any unmanaged resources it has held. 
Finalize() is used for the same purpose as dispose however finalize doesn’t assure the garbage collection of an object. 
Dispose() operates determinalistically due to which it is generally preferred.



What is the difference between Debug.Write and Trace.Write? When should each be used?


Debug.Write: Debug Mode, Release Mode, It used while debuging a project.
Trace.write: Release Mode, It used in Released verion of Applications.



Explain the use of virtual, sealed, override, and abstract.


The virtual keyword enables a class to be overridden. If it has to be prevented from being overridden, then the sealed keyword needs to be used. If the keyword virtual is not used, members of the class can even then be overridden. However, its usage is advised for making the code meaningful.


The override keyword is used to override the virtual method in the base class. Abstract keyword is used to modify a class, method or property declaration. You cannot instantiate an abstract class or make calls to an abstract method directly.


An abstract virtual method means that the definition of the method needs to be given in the derived class.



What benefit do you get from using a Primary Interop Assembly (PIA)?


A primary interop assembly contains type definitions (as metadata) of types implemented with COM. Only a single PIA can exist, which needs to be signed with a strong name by the publisher of the COM type library.
One PIA can wrap multiple versions of the same type library.


A COM type library imported as an assembly can be a PIA only if it has been signed and published by the same publisher.


Therefore, only the publisher of a type library can produce a true PIA, that can be considered as the unit of an official type definition for interoperating with the underlying COM types.



Explain the use of static members with example using C#.NET.


Static members are not associated with a particular instance of any class. 
They need to be qualified with the class name to be called. 
Since they are not associated with object instances, they do not have access to non-static members.
i.e.: "this" cannot be used, which represents the current object instance.





C# interview questions with answers, Part 2


C# developer interview questions with answers.


What is a delegate?

  1. A strongly typed function pointer.
  2. A light weight thread or process that can call a single method.
  3. A reference to an object in a different process.
  4. An inter-process message channel.
Answer :
1. A strongly typed function pointer.



How does assembly versioning in .NET prevent DLL Hell?

  1. The runtime checks to see that only one version of an assembly is on the machine at any one time.
  2. .NET allows assemblies to specify the name AND the version of any assemblies they need to run.
  3. The compiler offers compile time checking for backward compatibility.
  4. It doesn't.

Answer :
2. .NET allows assemblies to specify the name AND the version of any assemblies they need to run.



Which .Gang of Four. design pattern is shown below?
public class A {
    private A instance;
    private A() {
    }
    public
static A Instance {
        get
        {
            if ( A == null )
                A = new A();
            return instance;
        }
    }
}

  1. Factory
  2. Abstract Factory
  3. Singleton
  4. Builder


Answer :
3. Singleton



In the NUnit test framework, which attribute must adorn a test class in order for it to be picked up by the NUnit GUI?

  1. TestAttribute
  2. TestClassAttribute
  3. TestFixtureAttribute
  4. NUnitTestClassAttribute


Answer :
3. TestFixtureAttribute



Which of the following operations can you NOT perform on an ADO.NET DataSet?

  1. A DataSet can be synchronised with the database.
  2. A DataSet can be synchronised with a RecordSet.
  3. A DataSet can be converted to XML.
  4. You can infer the schema from a DataSet.

Answer :
2. A DataSet can be synchronised with a RecordSet.


In Object Oriented Programming, how would you describe encapsulation?

  1. The conversion of one type of object to another.
  2. The runtime resolution of method calls.
  3. The exposition of data.
  4. The separation of interface and implementation.
Answer :
4. The separation of interface and implementation.


C# interview questions with answers, Part 1

C# developer interview questions with answers.



The C# keyword .int. maps to which .NET type?

  1. System.Int16
  2. System.Int32
  3. System.Int64
  4. System.Int128

Answer : 
2. System.Int32


Which of these statements correctly declares a two-dimensional array in C#?

  1. int[,] myArray;
  2. int[][] myArray;
  3. int[2] myArray;
  4. System.Array[2] myArray;


Answer : 
1. int[,] myArray;




Which of these string definitions will prevent escaping on backslashes in C#?

  1. string s = #.n Test string.;
  2. string s = ..n Test string.;
  3. string s = @.n Test string.;
  4. string s = .n Test string.;
Answer : 
3. string s = @.n Test string.;



If a method is marked as protected internal who can access it?

  1. Classes that are both in the same assembly and derived from the declaring class.
  2. Only methods that are in the same class as the method in question.
  3. Internal methods can be only be called using reflection.
  4. Classes within the same assembly, and classes derived from the declaring class.


Answer : 
4. Classes within the same assembly, and classes derived from the declaring class.



What is boxing?

  1. Encapsulating an object in a value type.
  2. Encapsulating a copy of an object in a value type.
  3. Encapsulating a value type in an object.
  4. Encapsulating a copy of a value type in an object.


Answer : 
4. Encapsulating a copy of a value type in an object.



What compiler switch creates an xml file from the xml comments in the files in an assembly?

  1. /text
  2. /doc
  3. /xml
  4. /help
Answer : 
2. /doc


What is a satellite Assembly?

  1. A peripheral assembly designed to monitor permissions requests from an application.
  2. Any DLL file used by an EXE file.
  3. An assembly containing localized resources for another assembly.
  4. An assembly designed to alter the appearance or .skin. of an application.
Answer : 
3. An assembly containing localized resources for another assembly.


How to count vowels in a string using c#

You can find vowel's occurrence in a string by many ways, one of them is below :


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            string s1 = "This is test string";
            int count = findVowel(s1);
            Console.WriteLine( "No of vowels in '" + s1 + "' is : " + count); 


        }


        private static int findVowel(string str)
        {
            int count = 0;
            for (int i = 0; i < str.Length - 1; i++)
            {
                switch (str[i])
                {
                    case 'a':
                        count++;
                        break;


                    case 'A':
                        count++;
                        break;


                    case 'e':
                        count++;
                        break;


                    case 'E':
                        count++;
                        break;


                    case 'i':
                        count++;
                        break;


                    case 'I':
                        count++;
                        break;


                    case 'o':
                        count++;
                        break;


                    case 'O':
                        count++;
                        break;


                    case 'u':
                        count++;
                        break;


                    case 'U':
                        count++;
                        break;
                }  // end switch
                
            }  // end for loop


            return count;
        }


    }
}

Hit Like, if it help full to you... :)

const and readonly in C# .NET


What is the difference between const and readonly in C#.NET?


The read only can be modified by the class it is contained in. However, the const cannot be modified. It needs to be instantiated only at the compile time.

Generics in C# .NET


What are generics in C#.NET?


Generic types to maximize code reuse, type safety, and performance. They can be used to create collection classes. Generic collection classes in the System.Collections.Generic namespace should be used instead of classes such as ArrayList in the System.Collections namespace.


or


Generics allow you to define type-safe data structures, without committing to actual data types. It allows writing less but more effective code. Generic provides flexibility and power but the developer needs to be responsible while using it.