Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialRanvir Sahota
9,844 Pointswhy use Deserilize<T>(JsonReader)
In the video, the teacher says don't want to use Deserilize method that returns an object because she would have to cast it. So what are the differences between t and object their advantages and disadvantages? What were her reasons for not casting? I've been reading into boxing and unboxing and in general about t, but I feel like im missing something an explanation about this I believe will clear up my misunderstanding. Thank you.
5 Answers
James Churchill
Treehouse TeacherRanvir,
One of the advantages of open source software is that we can go read the source code to see what the differences are between two different methods. Here's a link to the implementation of the generic Deserialize<T>
method:
https://github.com/JamesNK/Newtonsoft.Json/blob/master/Src/Newtonsoft.Json/JsonSerializer.cs#L844
And here's the method implementation:
public T Deserialize<T>(JsonReader reader)
{
return (T)Deserialize(reader, typeof(T));
}
In this code snippet, we can see that the method delegates to the non-generic Deserialize
method and simply casts the object returned from that method to type of T
. Here's the implementation of the non-generic Deserialize
method:
public object Deserialize(JsonReader reader, Type objectType)
{
return DeserializeInternal(reader, objectType);
}
That method itself delegates to an internal method named DeserializeInternal
. Here's the link to that method in the source code:
https://github.com/JamesNK/Newtonsoft.Json/blob/master/Src/Newtonsoft.Json/JsonSerializer.cs#L861
So, as Steven speculated above, the generic Deserialize
method is really just a convenience method that saves you from having to do the cast yourself.
I hope this helps! Please let me know if you have any further questions.
Thanks ~James
Steven Parker
231,248 PointsI would not expect a functional difference, but it seems that using the generic is cleaner looking and a more modern technique. If you want a more definitive answer you could try tagging Carling and see if she responds.
Adham Ali
Courses Plus Student 1,112 Pointsto avoid boxing and unboxing. If you wanna understand this in depth(actually you gotta because you are a software developer)you should see both of this articles (in order):
1-An Introduction to C# Generics - MSDN - Microsoft:
This is an Introduction to gerneric types in C#
2-Generics (C# Programming Guide) | Microsoft Docs:
This is an Generic types in more depth in C#
Adham Ali
Courses Plus Student 1,112 PointsMr. James,
I mean that non-generic Deserialize method returns an object right? The generic one casts the object returned from non-generic Deserialize to the Type parameter specified between the angle brackets. the unboxing impact still there. That's what I`m trying to say. Thanks
James Churchill
Treehouse TeacherAdham,
Assuming that type T in Deserialize<T> is a reference type (and not a value type), then there isn't any boxing or unboxing occurring. Boxing only occurs when converting a value type to a type object (or to any interface type implemented by the value type).
Thanks ~James
Adham Ali
Courses Plus Student 1,112 PointsMr. James, Yeah yeah i got what u tryna mean ! Thanks, sir
Can i ask you a question? later in this course i couldn't do the bing subscriptions keys thing could u explain to me how to do it Keep in mind that the video is from 2015 or 2016 i dont know exactly and the design of the site is changed
Thanks!
Adham Ali
Courses Plus Student 1,112 PointsAdham Ali
Courses Plus Student 1,112 PointsWell in Deserialize<T> it seems that doesn`t solve any performance issues it casts the return type to a <T> which we would do actually if we use the non-generic Deserialize method. The unboxing impact is still there
James Churchill
Treehouse TeacherJames Churchill
Treehouse TeacherAdham,
Explicitly casting an object to another reference type (presumably a custom type defined by a class)--which is what is happening in the generic
Deserialize<T>
method--is not the same thing as boxing/unboxing. Boxing is the act of converting a value type to an object. Before we had the System.Collections.Generic namespace (introduced in .NET 2.0), this was a common performance issue developers had to deal with, as the available collections in the System.Collections namespace only worked with objects (see ArrayList https://msdn.microsoft.com/en-us/library/system.collections.arraylist(v=vs.110).aspx for an example).For more information about boxing and unboxing, see: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/types/boxing-and-unboxing
Thanks ~James