Tuesday, January 24, 2012

What’s the advantage of using System.Text.StringBuilder over System.String?

StringBuilder is more efficient in the cases, where a lot of manipulation is done to the text. Strings are immutable, so each time it’s being operated on, a new instance is created.

What is an ABC?

An abstract base class.
At the design level, an abstract base class (ABC) corresponds to an abstract concept. If you asked a mechanic if he repaired vehicles, he'd probably wonder what kind-of vehicle you had in mind. Chances are he doesn't repair space shuttles, ocean liners, bicycles, or nuclear submarines. The problem is that the term "vehicle" is an abstract concept (e.g., you can't build a "vehicle" unless you know what kind of vehicle to build). In C++, class Vehicle would be an ABC, with Bicycle, SpaceShuttle, etc, being derived classes (an OceanLiner is-a-kind-of-a Vehicle). In real-world OO, ABCs show up all over the place. 

At the programming language level, an ABC is a class that has one or more pure virtual member functions. You cannot make an object (instance) of an ABC.

What is a "virtual constructor"?

An idiom that allows you to do something that C++ doesn't directly support.
You can get the effect of a virtual constructor by a virtual clone() member function (for copy constructing), or a virtual create() member function (for the default constructor).
 class Shape {
 public:
   virtual ~Shape() { }                 
// A virtual destructor
   virtual void draw() = 0;             
// A pure virtual function
   virtual void move() = 0;
   
...
   virtual Shape* clone()  const = 0;   
// Uses the copy constructor
   virtual Shape* create() const = 0;   
// Uses the default constructor
 };

 class Circle : public Shape {
 public:
   Circle* clone()  const;   
// Covariant Return Types; see below
   Circle* create() const;   
// Covariant Return Types; see below
   
...
 };

 Circle* Circle::clone()  const { return new Circle(*this); }
 Circle* Circle::create() const { return new Circle();      } 
 

When should my destructor be virtual?

When someone will delete a derived-class object via a base-class pointer.
In particular, here's when you need to make your destructor virtual:
  • if someone will derive from your class,
  • and if someone will say new Derived, where Derived is derived from your class,
  • and if someone will say delete p, where the actual object's type is Derived but the pointer p's type is your class.

How can a member function in my derived class call the same function from its base class?

Use Base::f();
Let's start with a simple case. When you call a non-virtual function, the compiler obviously doesn't use the virtual-function mechanism. Instead it calls the function by name, using the fully qualified name of the member function. For instance, the following C++ code...
 void mycode(Fred* p)
 {
   p->goBowling();  
 pretend Fred::goBowling() is non-virtual
 }
...might get compiled into something like this C-like code (the p parameter becomes the this object within the member function):
 void mycode(Fred* p)
 {
   __Fred__goBowling(p);  
 pseudo-code only; not real
 } 
 

What happens in the hardware when I call a virtual function? How many layers of indirection are there? How much overhead is there?

The answer is entirely compiler-dependent, so your mileage may vary, but most C++ compilers use a scheme similar to the one presented here.
Let's work an example. Suppose class Base has 5 virtual functions: virt0() through virt4().
 // Your original C++ source code

 class Base {
 public:
   virtual 
arbitrary_return_type virt0(...arbitrary params...);
   virtual 
arbitrary_return_type virt1(...arbitrary params...);
   virtual 
arbitrary_return_type virt2(...arbitrary params...);
   virtual 
arbitrary_return_type virt3(...arbitrary params...);
   virtual 
arbitrary_return_type virt4(...arbitrary params...);
   
...
 }; 

What's the difference between how virtual and non-virtual member functions are called?

Non-virtual member functions are resolved statically. That is, the member function is selected statically (at compile-time) based on the type of the pointer (or reference) to the object. 

In contrast, virtual member functions are resolved dynamically (at run-time). That is, the member function is selected dynamically (at run-time) based on the type of the object, not the type of the pointer/reference to that object. This is called "dynamic binding." Most compilers use some variant of the following technique: if the object has one or more virtual functions, the compiler puts a hidden pointer in the object called a "virtual-pointer" or "v-pointer." This v-pointer points to a global table called the "virtual-table" or "v-table." 

How can C++ achieve dynamic binding yet also static typing?

When you have a pointer to an object, the object may actually be of a class that is derived from the class of the pointer (e.g., a Vehicle* that is actually pointing to a Car object; this is called "polymorphism"). Thus there are two types: the (static) type of the pointer (Vehicle, in this case), and the (dynamic) type of the pointed-to object (Car, in this case). 

What is a "virtual member function"?

A virtual function allows derived classes to replace the implementation provided by the base class. The compiler makes sure the replacement is always called whenever the object in question is actually of the derived class, even if the object is accessed by a base pointer rather than a derived pointer. This allows algorithms in the base class to be replaced in the derived class, even if users don't know about the derived class.

What is the difference betweein pointer to constant or a constant pointer?

Lets Take an example :
We have :

char *const p1="Shashwat";
char const* p2="Shriparv";

How do I call a C++ function from C?

Just declare the C++ function ``extern "C"'' (in your C++ code) and call it (from your C or C++ code). For example:
 // C++ code:

 extern "C" void f(int);

 void f(int i)
 {
  // ...
 }
Now f() can be used like this:
 /* C code: */

 void f(int);
 
 void cc(int i)
 {
  f(i);
  /* ... */
 }

Why can't I overload dot, ::, sizeof, etc.?

Most operators can be overloaded by a programmer. The exceptions are
 . (dot)  ::  ?:  sizeof
There is no fundamental reason to disallow overloading of ?:. I just didn't see the need to introduce the special case of overloading a ternary operator. Note that a function overloading expr1?expr2:expr3 would not be able to guarantee that only one of expr2 and expr3 was executed. Sizeof cannot be overloaded because built-in operations, such as incrementing a pointer into an array implicitly depends on it. Consider:
 X a[10];
 X* p = &a[3];
 X* q = &a[3];
 p++; // p points to a[4]
  // thus the integer value of p must be
  // sizeof(X) larger than the integer value of q

What is the difference between new and malloc()?

malloc() is a function that takes a number (of bytes) as its argument; it returns a void* pointing to unitialized storage. new is an operator that takes a type and (optionally) a set of initializers for that type as its arguments; it returns a pointer to an (optionally) initialized object of its type. The difference is most obvious when you want to allocate an object of a user-defined type with non-trivial initialization semantics. Examples:
 class Circle : public Shape {
 public:
  Cicle(Point c, int r);
  // no default constructor
  // ...
 }; 
 

What's the value of i++ + i++?

It's undefined. Basically, in C and C++, if you read a variable twice in an expression where you also write it, the result is undefined. Don't do that. Another example is:
 v[i] = i++;
Related example:
 f(v[i],i++);
Here, the result is undefined because the order of evaluation of function arguments are undefined. Having the order of evaluation undefined is claimed to yield better performing code. Compilers could warn about such examples, which are typically subtle bugs (or potential subtle bugs). I'm disappointed that after decades, most compilers still don't warn, leaving that job to specialized, separate, and underused tools.

Thursday, January 19, 2012

How to: Add a Content Type to a SharePoint List using C#

Create a SharePoint 2010 Project

In this task, you create an empty SharePoint 2010 project in Microsoft Visual Studio 2010.
To create the SharePoint project
  1. To start Visual Studio 2010, click the Start Menu, click All Programs, click Microsoft Visual Studio 2010, and then click Microsoft Visual Studio 2010.
  2. On the File menu, point to New, and then click Project.
  3. In the New Project dialog window, in the Installed Templates section, click Visual C#, click SharePoint, and then click2010.
  4. Select Empty SharePoint Project from the project items.
  5. In the Name box, type CreateContentType and then click OK.
  6. In the SharePoint Customization Wizard, type the local Web site that you want to use for this exercise (such as http://localhost/SampleWebSite).
  7. For the trust level, select Deploy as a farm solution and then click Finish.

Create a Content Type

Monday, January 16, 2012

What are Satellite Assemblies?


A .NET Framework assembly containing resources specific to a given language. Using satellite assemblies, you can place the resources for different languages in different assemblies, and the correct assembly is loaded into memory only if the user selects to view the application in that language. In general, assemblies should contain culture-neutral resources. If you want to localize your assembly (for example use different strings for different locales) you should use satellite assemblies.




This means that you develop your application in a default language and add flexibility to react with change in the locale. Say, for example, you developed your application in an en-US locale. Now, your application has multilingual support. When you deploy your code in, say, India, you want to show labels, messages shown in the national language which is other than English.
Satellite assemblies give this flexibility. You create any simple text file with translated strings, create resources, and put them into the bin\debug folder. That's it. The next time, your code will read the CurrentCulture property of the current thread and accordingly load the appropriate resource.

Tell Something about class/object life cycle...





  1. Load the assembly
  2. Execute static initialisers
  3. "new" call:
    • allocate memory
    • execute non-static initialisers
    • execute constructor
  4. the instance is now ready to be used
  5. after the last reference to the object has vanished: if the object has no finalizer, it is now ready for collection; if the object has a finalizer, it is put on the finalizer queue.
  6. (optional) the objects from the finalizer queue have their finalizer called in a special thread; if there is still no reference from the application to the object, it too becomes now eligible for garbage collection
  7. the garbage collector deallocates memory

Sunday, January 15, 2012

What is Singleton Class in C++?

A class whose number of instances that can be instantiated is limited to one is called a singleton class. Thus, at any given time only one instance can exist, no more.

 The singleton design pattern is used whenever the design requires only one instance of a class. Some examples:
  • Application classes. There should only be one application class. 
  • Logger classes. For logging purposes of an application there is usually one logger instance required.
There are several ways of creating a singleton class. The most simple approach is shown below:


class Singleton
{
public:
static Singleton& Instance()
{ static Singleton singleton;
return singleton;
}
// Other non-static member functions
private:
Singleton() {}; // Private constructor
Singleton(const Singleton&); // Prevent copy-construction
Singleton& operator=(const Singleton&); // Prevent assignment
};



Saturday, January 14, 2012

What is difference between dot net framework 3.5 and 4.0?



ASP.NET 3.5 is having the following main features which are not availablle in the prior releases


1) AJAX integration

2) LINQ
3) Automatic Properties
4) Lambda expressions



I hope it would be useful for everyone to know about the differences about asp.net 3.5 and its next version asp.net 4.0



Because of space consumption I'll list only some of them here.



1) Client Data access:


ASP.NET 3.5: There is no direct method to access data from client side. We can go for any of these methods


Tell something about View State in Asp.Net

This is used by an ASP.NET Web page to persist changes to the state of a Web Form across postbacks.  The view state of a page is, by default, placed in a hidden form field named __VIEWSTATE. The load view state stage only happens when the page has been posted back. During this stage, the view state data that had been saved from the previous page visit is loaded and recursively populated into the control hierarchy of the Page. It is during this stage that the view state is validated.  It provide state to a webpage.


As we know A Web application is stateless. A new instance of the Web page class is created every time that the page is requested from the server. This would ordinarily mean that all information in the page and in its controls would be lost with each round trip. For example, by default if a user enters information into a text box on an HTML Web page, that information is sent to the server. However, it is not returned to the browser in the response.
To overcome this intrinsic limitation of Web programming, the ASP.NET page framework includes several state-management features to preserve page and control values between round trips to the Web server.


What Happens with View State

What is post back in Asp.Net


PostBack is the name given to the process of submitting an ASP.NET page to the server for processing . PostBack is done if certain credentials of the page are to be checked against a database (such as verification of username and password). This is something that a client machine is not able to accomplish and thus these details have to be ‘posted back’ to the server.
A simple example to illustrate the usage of PostBack is a login page. After the user has typed his username and password, he clicks on the ‘Login’ button. Upon the click, the page is sent to the server to check against the database/XML file to check if the user with supplied details is an authenticated user.
Then there arise certain events ( Listbox Index Changed,RadioButton Checked etc..) in an ASP.NET page upon which a PostBack might be needed. Consider the case in which you have 2 ComboBoxes. Let us say the first ComboBox asks you for the Country you reside in and the second one asks you for the State/Province in that country. Based upon the Country you select, the list of States/Provinces must be shown. Thus, in order to fill the values in the second ComboBox, the selection in the first ComboBox must be known to the server. Thus, as and when an item is selected in the first ComboBox, a PostBack must be done and the appropriate list of items must be filled in the second ComboBox.

How page request is handled in Asp.Net

ASP.NET Page Handling








Click here for larger image.




ms972976.viewstate_fig02(en-us,MSDN.10).gif


When to use Abstract Class and when to use Interface in project?

Use interface when you do not want to give any implementation and use abstract class when you want to give some common implementation that would be used by classes derived from that abstract class. An abstract class can be skeleton implementation of existing interface. 




Use abstract clas for "is A" relationship,  while use interfaces for "Can do" relationship.

Example:



Live

Your Ad Here