Monday, February 23, 2009

const / read-only / static in C#

  • It is defined at compile time and cannot be changed at runtime and must be initialized as they are declared.
    E.g. public const double PI = 3.14159;
    Changing the value elsewhere this will cause a compiler error.
  • Constants must be of an integral type (sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double, decimal, bool, or string), an enumeration, or a reference to null.
  • Since classes or structures are initialized at run time with the new keyword, and not at compile time, you can't set a constant to a class or structure.
  • Constants can be marked as public, private, protected, internal, or protected internal.
  • Constants are accessed as if they were static fields, although they cannot use the static keyword.
  • To use a constant outside of the class that it is declared in, you must fully qualify it using the class name.
  • Can be used in attributes
  • Are copied into every assembly that uses them (every assembly gets a local copy of values)
  • Could be declared within functions
  • The compiler performs some optimization by not declaring any stack space for the field
  • It is like a constant in that it represents an unchanging value. The difference is that a readonly member can be initialized at runtime, in a constructor as well being able to be initialized as they are declared.


    public class MyClass
    public readonly double PI = 3.14159;


    public class MyClass
    public readonly double PI;

    public MyClass()
    PI = 3.14159;
  • Because a readonly field can be initialized either at the declaration or in a constructor, readonly fields can have different values depending on the constructor used.
  • A readonly field can also be used for runtime constants.
    E.g. public static readonly uint l1 = (uint)DateTime.Now.Ticks;
  • readonly members are not implicitly static, and therefore the static keyword can be applied to a readonly field explicitly if required.
  • A readonly member can hold a complex object by using the new keyword at initialization.
  • readonly members cannot hold enumerations.
  • Use of the static modifier to declare a static member, means that the member is no longer tied to a specific object.
  • This means that the member can be accessed without creating an instance of the class.
  • Only one copy of static fields and events exists, and static methods and properties can only access static fields and static events. E.g. public static int NumberOfWheels = 4;
  • The static modifier can be used with classes, fields, methods, properties, operators, events and constructors, but cannot be used with indexers, destructors, or types other than classes.
  • static members are initialized before the static member is accessed for the first time, and before the static constructor, if any is called.
  • To access a static class member, use the name of the class instead of a variable name to specify the location of the member. E.g., int i = Car.NumberOfWheels;
Note: There are some small performance gains to be realized from using const instead of readonly, but you give up quite a bit of flexibility. You’ll need to recompile every assembly that uses a const value. In the case of readonly you need only update the definition. This flexibility greatly overrides the minimal performance gains from using const as the key. Minimize your use of const to attribute parameters and enums. Everything else should be declared readonly instead.

Saturday, February 21, 2009

YAHOO BOOKMARK - Best online Bookmarking site

Bookmarking is the best way to collect cool Web sites used to be to save them to your browser's menu of bookmarks or favorites. But the more you saved, the easier it was to lose track of the pages within an ever-growing list of folders.

YAHOO BOOKMARK is the most polished bookmark manager. It’s a neat and convenient way of storing your bookmarks online. All you need to have is a free Yahoo Account.


1. It is perfect for saving all your Bookmarks and Favorites Online. It acts and feels very much like your browser's built-in bookmark manager.
2. You can quickly access your Bookmarks and Favorites from any computer, anywhere in the world. You will never lose a site again.
3. There is a search feature. Even you can tag them with topics for easy searches.
5. Yahoo doesn’t let you share bookmarks with other internet users. It is not intended to be a social bookmark site like the others. Instead, it essentially allows you use your personal bookmark list at any computer.
6. You can seamlessly import your current bookmarks or export your Yahoo bookmarks using standard formats to import into any browser. It imported our existing Favorites, from Internet Explorer or Netscape, and presented them in the usual way. That includes retaining the original folders so you don’t have to waste any time re-filing them. Naturally you can add or manage those folders so that future bookmarks can be filed as you wish.
7. The service’s other impressive feature is the recently-added ability to export your Favorites out of Yahoo Bookmarks, making it a useful back-up service.
8. You can create multi-level folder(tree) structure and can organize your bookmarks more efficiently.
9. In some of the bookmarking sites, you always have to login again and again due to session end, but here you just need to login once and can use this toll untill you close your browser.
10. Yahoo Bookmarks is only really helpful if you have the Yahoo toolbar installed. You can add any sites in just one single click.

Things to improve:

1. When one click on a link, it open the link in the same window. There is no choice to open a link in a new window.
2. There is an extra level of redirection with each click. First it redirects to an internal Yahoo page, then to the actual URL.
3. Search is little bit slow and when try to edit a searched item, it is not showing the category where the particular link is placed.

Sunday, February 15, 2009

Best quotes I came across

Work like you don’t need money, love like you’ve never been hurt, and dance like no one’s watching

The person who reads too much and uses his brain too little will fall into lazy habits of thinking.
—Albert Einstein

Advice is what we ask for when we already know the answer but wish we didn’t.
—Erica Jong

You can do anything, but not everything.
—David Allen

Deal with the faults of others as gently as with your own. --- Chinese proverb

You cannot put the same shoe on every foot. --- Publilus Syrus

It's never too late -- never too late to start over, never too late to be happy." --- Jane Fonda

Management is doing things right; leadership is doing the right things.
Peter F. Drucker

Don't tell people how to do things, tell them what to do and let them surprise you with their results. - George S. Patton

The best time to plant a tree is twenty years ago. The second best time is now.