ScholarQuill logoScholarQuillUniversity Notes
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
Study Tools
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
Contact
ScholarQuill logo
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
ScholarQuill logoScholarQuillUniversity Notes
Study PortalEC-332Csharp Boxing Unboxing
Ctrl + K
Login
Explore
Search
Semester
Papers
CGPA
    EC-332

    Enterprise Application Development

    Progress0 / 37 done
    Topics37
    1Microsoft technology history2Introduction to .NET and its architecture3Concept of MSIL, CLR, CLS, CTS
    4Managed and Unmanaged Code5.Net Assembly6Introduction to C# fundamentals7Boxing and Unboxing8Implementing multi-tier architecture
    9SQL Injection, parameterized queries10Usage of data set, Data adapter and command builder in disconnected model
    11Multicast delegates12Introduction to windows forms13HTML
    14javascript and its data types, variables, functions15Debugging javascript using Firebug
    16Browser's Object (BOM), Document Object Model
    17Jquery effects
    18LINQ to Objects, LINQ to SQL19Query syntax, Operations (projection, filtering and join) using Linq Queries
    20The entity data model, CSDL21Eager vs lazy loading, POCO classes, DBContext API22Querying entity data models23Introduction to ASP.NET MVC24MVC application structure, Controllers overview25Action Methods, Parameterized action methods26Introduction to razor syntax27Code expressions, Code Blocks, Implicit Vs Explicit Code Expression28Data annotations, Client and Server Side Validation29Validation and model binding, Validation and model state30MVC Membership, Authorization and security
    31SOAP, WSDL32Service contract, Data contract, XML, WCF bindings33ABC of WCF, Restful services34Consuming rest services (CRUD operations) using Jquery AJAX and JSON35Introduction to web API36Example of web API using CRUD Example37MVC routing
    4 min read
    Topic 7 of 37
    Topic Study Note Illustration
    07

    Boxing and Unboxing

    Boxing and Unboxing are concepts in C# related to the conversion between value types and reference types . These operations allow value types (like int , char...

    Boxing and Unboxing are concepts in C# related to the conversion between value types and reference types. These operations allow value types (like int, char, etc.) to be treated as objects, and vice versa.

    1. Boxing

    Boxing is the process of converting a value type (such as int, double, char, etc.) into a reference type (specifically, an object in C#). This allows value types to be treated as objects, which is necessary when they need to be stored in collections that work with objects, like ArrayList or when passing them to methods that expect object parameters.

    When a value type is boxed, the CLR (Common Language Runtime) creates a heap object to store the value, which is then wrapped inside a reference type (the object).

    Example of Boxing:

    int num = 123; // value type (stack)
    object obj = num; // Boxing: num is now a reference type (heap)
    
    Console.WriteLine(obj);  // Output: 123
    

    In this example:

    • The value 123 is a value type (stored on the stack).
    • The value 123 is boxed into an object, which is a reference type (stored on the heap).

    Boxing is an implicit operation, meaning it happens automatically when a value type is assigned to an object.

    2. Unboxing

    Unboxing is the process of converting a boxed object back into a value type. This requires explicit casting, as the CLR needs to retrieve the original value from the object reference.

    Unboxing is a manual process and involves extracting the value from the boxed object. If the type of the object being unboxed is incompatible with the type being cast to, an InvalidCastException will occur.

    Example of Unboxing:

    object obj = 123; // Boxing happens here
    int num = (int)obj; // Unboxing: explicit cast to the original value type
    
    Console.WriteLine(num);  // Output: 123
    

    In this example:

    • The integer 123 was boxed into an object.
    • The boxed value is then unboxed back into an int using an explicit cast (int)obj.

    Key Points to Remember

    1. Boxing:

      • Implicit operation: Automatically converts a value type to an object.
      • It involves copying the value to the heap, which can be less efficient due to the extra memory allocation.
    2. Unboxing:

      • Explicit operation: Requires a cast to retrieve the value type from the object.
      • Can throw an InvalidCastException if the types don’t match.

    Performance Considerations

    • Boxing and unboxing can have a performance impact, especially in scenarios where large numbers of value types need to be boxed and unboxed repeatedly.
    • When boxing a value type, the value is stored on the heap, which involves extra memory management overhead (compared to storing data on the stack).
    • Unboxing requires a type check, which adds some overhead.

    Practical Use Cases

    • Boxing and Unboxing in Collections: Collections like ArrayList in earlier versions of .NET could only store object types, so value types had to be boxed to be added to the collection. However, with generic collections in modern C#, such as List<T>, boxing and unboxing are no longer required for value types, as the collection can directly store the specific type.

      // Example using a generic collection (no boxing required)
      List<int> numbers = new List<int>();
      numbers.Add(123);  // No boxing
      
    • Boxing for Polymorphism: Boxing can be used to take advantage of polymorphism. If a value type is needed in a context where an object is required (like in some collections), boxing allows that value type to be treated as an object.

    Summary of Boxing and Unboxing

    Concept Description
    Boxing Converting a value type to a reference type (object). It’s an implicit operation.
    Unboxing Converting a reference type back to its original value type. It’s an explicit operation that requires casting.
    Performance Boxing and unboxing incur additional overhead, which may affect performance in large-scale operations or repeated conversions.

    In general, it's important to avoid excessive boxing and unboxing in performance-critical code. Using generics in C# (like List<T>) helps avoid these operations and ensures better performance.

    ← Previous topic 6
    Introduction to C# fundamentals
    Next topic 8 →
    Implementing multi-tier architecture

    Past Papers

    Open this section to view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    4mremaining
    673words
    Beginnerlevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Enterprise Application Development