what is the use of object keyword. RRS feed

  • Question

  • User-712926555 posted

    what is the use of object keyword and where i can use it. 

    Saturday, July 28, 2012 1:21 PM


All replies

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, July 28, 2012 1:26 PM
  • User-434868552 posted

    @ Hisanth

    the c# reference is one of your best friends:

    4.2.2 The object type
    The object class type is the ultimate base class of all other types.
    Every type in C# directly or indirectly derives from the object class type.

    The keyword object is simply an alias for the predefined class System.Object.

    what is the use of object keyword ...

    you may want to read sections 4.3 Boxing and unboxing and 4.7 The dynamic type

    ... and where i can use it


    Performance Considerations
    If you are designing a class, such as a collection, that must handle any type of object, you can create class members that accept instances of the Object class.

    However, the process of boxing and unboxing a type carries a performance cost. If you know your new class will frequently handle certain value types you can use one of two tactics to minimize the cost of boxing.

    • One tactic is to create a general method that accepts an Object type, and a set of type-specific method overloads that accept each value type you expect your class to frequently handle. If a type-specific method exists that accepts the calling parameter type, no boxing occurs and the type-specific method is invoked. If there is no method argument that matches the calling parameter type, the parameter is boxed and the general method is invoked.
    • The other tactic is to design your class and its methods to use generics. The common language runtime creates a closed generic type when you create an instance of your class and specify a generic type argument. The generic method is type-specific and can be invoked without boxing the calling parameter.

    Although it is sometimes necessary to develop general purpose classes that accept and return Object types, you can improve performance by also providing a type-specific class to handle a frequently used type.

    For example, providing a class that is specific to setting and getting Boolean values eliminates the cost of boxing and unboxing Boolean values.

    Hisanth, you best option may be to avoid directly using System.Object until your intuition or some other circumstance shows you that using System.Object directly is appropriate; too often imho, programmers use things that they do not fully understand -- frequently, this can be a poor design choice.

    TIMTOWTDI  =.   there is more than one way to do it


    N.B.:  imho, it's worth your while to refer often to the c@ language specification.
                http://msdn.microsoft.com/en-us/library/ms228593.aspx "C# Language Specification"

    Saturday, July 28, 2012 3:12 PM
  • User-821857111 posted

    This forum is intended for discussion about C# issues arising from ASP.NET development. It is not a general C# discussion forum. Questions of this type should be asked over at MSDN: http://social.msdn.microsoft.com/Forums/en/category/visualcsharp

    Sunday, July 29, 2012 4:26 AM