none
What are these parentheses and how do I use them? RRS feed

  • Question

  • So I am currently learning C# and in the textbook I came across this block of code while going through Classes.

    class Employee : IComparable
    {
     public int IdNumber {get; set;}
     public double Salary {get; set;}
     int IComparable.CompareTo(Object o)
     {
     int returnVal;
     Employee temp = (Employee)o;
     if(this.IdNumber > temp.IdNumber)
     returnVal = 1;
     else
     if(this.IdNumber < temp.IdNumber)
     returnVal = -1;
     else
     returnVal = 0;
     return returnVal;
     }
    }

    In line 8 where it says:

    Employee temp = (Employee)o;

    it seems to bind the object "o" to the class "Employee". In the entire textbook I have not seen the use of parenthesis like this and I would like an explanation and I feel like this is the best place to ask...

    Thanks in advance
     - Michael

    Tuesday, January 8, 2019 7:33 PM

All replies

  • The parentheses are used to cast (in this case) o to type Employee. See the following documentation.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Tuesday, January 8, 2019 7:49 PM
    Moderator
  • This construction is called a "cast". Basically it means "treat the object on the right hand side as the type that is written between parenthesis".

    In the example

    Employee temp = (Employee)o;

    it means "Take the object o and treat it as an Employee so that we can assign it to a variable of type Employee". If at the time of execution the variable o does not contain an Employee (or something that derives from Employee), it will throw an exception.

    If you do not use the cast, and try to do this:

    Employee temp = o;

    then you will get a compile-time error saying that you cannot assign Object to Employee. When you prefix it with (Employee) you are telling the compiler "ok, I know that o will contain an Employee at the time of execution, so allow me to do this assignment which would otherwise cause an error".

    Tuesday, January 8, 2019 8:30 PM
    Moderator
  • You are dealing with the Employee object. But the Employee object is being held in a container  object "Object".

    int IComparable.CompareTo(Object o)

    Object is the base object that all .NET objects are derived from, and Object can be used as a container to hold a user defined object like the Employee object.

    https://docs.microsoft.com/en-us/dotnet/api/system.object?view=netframework-4.7.2

    <copied>

    Because all classes in the .NET Framework are derived from Object, every method defined in the Object class is available in all objects in the system.  

    <end>

    So what it really means is this.

    Object o = Employee;

    And now, you can pass the 'o' base around, but 'o' base object  is just a container for the Employee object.

    In order the turn the 'o' base object back into an Employee object, the 'o' base object has to be cast back to Employee object

    Employee temp = (Employee)o;

    Temp object is really an Employee object.

    You can do this too.

    var temp = (Employee)o;

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/implicitly-typed-local-variables

    Temp is still an Employee object when var is used too.

    Capiche?



    • Edited by DA924x Thursday, January 10, 2019 10:48 AM
    Tuesday, January 8, 2019 10:08 PM
  • I am too tired right now to comprehend DA924x's explanation, but it seems in depth and I like that so I am gonna wait until I am not exhausted to do that however (comma), I all answer were helpful and I get the gist of what it's doing now, one last question. Are there any other situations I can cast it like this or can I only use it in that scenario? @Kareninstructor's link did have a few examples where this method was used on variables.

    Basically when should I use this practice and when shouldn't I use it. Also what are it's limits, what are the criteria for you to be able to cast?

    Thursday, January 10, 2019 4:12 AM
  • Basically when should I use this practice and when shouldn't I use it. Also what are it's limits, what are the criteria for you to be able to cast?

    When you know what the object should be a type that you should be casting an object to is when you do the cast.

    Here is an example where the textbox change event is a generic event that all textboxes on the form use the one generic event.

    private void TextBox_TextChanged(object sender, TextChangedEventArgs e) { // ... Get control that raised this event. var textBox = sender as TextBox;

    // or you could do this var textBox = (Textbox)sender; if (textBox.Name == "Textbox1") { // ... Change Window Title. this.Title = textBox.Text + "[Length = " + textBox.Text.Length.ToString() + "]"; }

    else

    {

    // do something else becuase it is not Textbox1

    } }



    • Edited by DA924x Thursday, January 10, 2019 6:47 AM
    Thursday, January 10, 2019 6:45 AM
  • In addition to my first reply, digging deeper I would recommend reading the following (which is a good discussion on casting safely).

    Casting vs "as" - Embracing exceptions.

    Using as and is for safe casting in C#.

    Now there is one more thing to consider when writing code where casting is involved, to cover all your bases in general unit test should be written for your code so that the unexpected does not happen. This includes trying to break your code rather than simply going down a "happy path". What happens if you cast an object to a specific type then don't account for a property not in a working condition; suppose you are using Entity Framework and turn lazy loading off to read customer data then attempt to read order data in a following lambda statement which has navigation properties for order details and want to access say order details, all appears fine until you attempt to access order details. All casting and type checking gets the thumbs up but an exception is thrown. So the follow up in this case is to do a conditional null check.

    Hopefully this information is useful and assist in better understanding casting and what also needs to be considered.

    In closing I believe all those who have replied have given you enough to make an informed decision on how to proceed but I highly encourage you to "play" with both good and bad pieces of code so you can write good code that anticipates invalid conditions at runtime. 


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, January 10, 2019 11:29 AM
    Moderator