ScholarQuill logoScholarQuillUniversity Notes
  • Notes
  • Past Papers
  • Blogs
  • Todo
Login
ScholarQuill logoScholarQuillUniversity Notes
Login
NotesPast PapersBlogsTodo
More
SubjectsDiscussionCGPA CalculatorGPA CalculatorStudent PortalCourse Outline
About
About usPrivacy PolicyReportContact
Notes
Past Papers
Blogs
Todo
Analytics
    Current Subject
    🧩
    Enterprise Application Development
    EC-332
    Progress0 / 37 topics
    Topics
    1. Overview of Enterprise Application Development: Microsoft technology history2. Introduction to .NET and its architecture3. Concept of MSIL, CLR, CLS, CTS4. Introduction to .NET framework: Managed and Unmanaged Code5. .Net Assembly6. Introduction to C# fundamentals7. Boxing and Unboxing8. Implementing multi-tier architecture9. Introduction to ADO.Net: SQL Injection, parameterized queries10. Usage of data set, Data adapter and command builder in disconnected model11. Introduction to delegate: Multicast delegates12. Introduction to windows forms13. HTML14. Introduction to javascript: javascript and its data types, variables, functions15. Debugging javascript using Firebug16. Introduction to various object models: Browser's Object (BOM), Document Object Model17. Introduction to Jquery: Jquery effects18. Introducing LINQ: LINQ to Objects, LINQ to SQL19. Query syntax, Operations (projection, filtering and join) using Linq Queries20. Introduction to ADO.NET entity framework: The entity data model, CSDL21. Eager vs lazy loading, POCO classes, DBContext API22. Querying entity data models23. Introduction to ASP.NET MVC24. MVC application structure, Controllers overview25. Action Methods, Parameterized action methods26. Introduction to razor syntax27. Code expressions, Code Blocks, Implicit Vs Explicit Code Expression28. Data annotations, Client and Server Side Validation29. Validation and model binding, Validation and model state30. MVC Membership, Authorization and security31. Introduction to service-oriented architecture: SOAP, WSDL32. Service contract, Data contract, XML, WCF bindings33. ABC of WCF, Restful services34. Consuming rest services (CRUD operations) using Jquery AJAX and JSON35. Introduction to web API36. Example of web API using CRUD Example37. MVC routing
    EC-332›Boxing and Unboxing
    Enterprise Application DevelopmentTopic 7 of 37

    Boxing and Unboxing

    4 minread
    673words
    Beginnerlevel

    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 load past papers

    Click on Show Past Papers to see past papers.
    On This Page
      Reading Stats
      Est. reading time4 min
      Word count673
      Code examples0
      DifficultyBeginner