Type Comparisons, Coversion , Boxing and Unboxing

Type Comparisons



Once you have a variable, you inevitably want to compare it with another variable using a comparison operator (==, !=, and so forth). However, you need to be aware that type comparison behavior varies, depending on whether you’re comparing two value types or two reference types. Value type comparisons are as you would expect them: the comparison returns true if the values held are identical. Reference type comparisons generally return true if the variables point to the same object. Therefore, the comparison of two objects that contain the same data would not be considered equal.

Interestingly, although string is a reference type, as we saw previously with assignment, string behaves as a value type. The equality operator is overridden so a string comparison returns true if the values being referred to are identical. String also includes a static Compare method that can be used to perform case-insensitive comparisons.

Boxing and Unboxing


Boxing is the name of the technique C# uses to convert a value type to object (or System.Object) and unboxing is the name given to the conversion from an object back to a value type. Any type, value, or reference can be assigned to an object without an explicit conversion. If the source for the assignment is a value type, C# allocates heap for the value’s data, and then assigns the reference to that data to the object. Boxing is used whenever a conversion to object is required, either because of explicit assignment or because of parameters being passed to a method call. Unboxing moves a value type from an object reference to a value type. Note that conversion rules apply to this assignment. In other words, a value type cannot be unboxed to a different type without an explicit cast.

using System;
class BoxingSample
{
        public static void Main()
       {
            // box
            object objNum;
            int intNumber= 300;
            objNum = intNumber;
           Console.WriteLine("o is {0}, type is {1}", o, o.GetType());
           // now we unbox.
           int intCopy;
           intCopy= (int)objNum;
           intCopy+= 5;
           Console.WriteLine("Copy is {0}", intCopy);
     }
}

Boxing preserves type safety and performance with an object-based type system.

Type Conversions


C# supports both implicit and explicit type conversions. Implicit conversions occur without requiring a cast, while explicit conversions require a cast. The compiler generates an error if an attempt is made to perform a supported explicit conversion without a cast operator or if an illegal explicit cast is attempted. For a listing of the valid conversions, check the Framework SDK documentation.

The conversion operation can be overridden so your custom classes can provide either implicit or explicit conversions. It is recommended that conversions which can have unintended side effects be overridden explicitly. The explicit cast should be a signal to the user that the conversion may not be perfect. Casting a long to an integer type, for example.

No comments:

Post a Comment

Labels

.NET Framework Interview Questions (7) .NET Interview Questions (10) .NET Remoting Interview Questions (1) ADO.NET and BLOB Error (1) ADO.NET Interview Questions (4) Agile Articles (9) AJAX Articles (5) AJAX Interview Questions (11) Algorithms (2) Analytics Articles (2) Analytics Interview Questions (3) Android FAQs - Part 1 (2) Articles (13) ASP.NET Articles (24) ASP.NET Error and Resolution (4) ASP.NET Interview Questions (23) ASP.NET Tutorial (8) AWS Interview Questions (16) Business Analyst Interview Questions (1) Cloud Computing Interview Questions (16) CSharp Articles (17) CSharp Interview Questions (32) CSharp Tutorial (17) Data Analysis (2) Data Structure (1) Design Pattern Articles (5) DevOps Tutorial (1) Digital Marketing Interview Questions (1) Download Templates (1) Error Resolution (6) Excel Articles (9) Excel Macros (1) Excel Tips and Tricks (10) HTML5 Interview Questions (3) HTML5 Tutorial (3) Interview Preparation (2) Interview Questions (24) Introduction to Business Analytics (10) Introduction to Python (7) Introduction to R Programming (23) JAVA Articles (6) Java Tutorial (5) LINQ Articles (4) LINQ Interview Questions (2) LINQ Tutorial (3) Microservices Interview Questions (1) MVCInterviewQuestions (2) OOPs Interview Questions (4) Oracle 9i Tutorial (14) Oracle Articles (2) Oracle Interview Questions (15) Outlook Error (1) PHP Interview Questions (3) PHP Tutorial (3) Product Management (12) Product Management Interview Questions (14) Product Owner Interview Questions (2) Program Management (5) Project Management (13) Project Management Articles (34) Project Management Interview Questions (25) Quiz (1) RallyDev Help (1) Scrum Master Interview Questions (11) Selenium Tutorial (1) Sharepoint Articles (1) SQL Interview Questions (23) SQL Server Articles (20) SSIS Interview Questions (6) SSRS Interview Questions (1) Technical Program Management (12) Technical Program Management - Interview Questions (24) TechnicalProgramManagement (5) Threading Interview Questions (2) Tutorial (8) UML Articles (3) UML Interview Questions (2) Unix (3) UNIX Tutorial (3) WCF Articles (20) WCF Interview Questions (9) WCF Quiz (2) WCF Tutorial (16) Web Service Articles (5) Web Service Interview Questions (3) Window Azure (1) XML Articles (6) XML Interview Questions (3) XML Tutorial (3)