Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6468175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T05:51:56+00:00 2026-05-25T05:51:56+00:00

Consider the following: TextReader reader = new StreamReader(file); XmlSerializer xmlSerializer = new XmlSerializer(typeof(T)); return

  • 0

Consider the following:

        TextReader reader = new StreamReader(file);
        XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
        return (T)xmlSerializer.Deserialize(reader);

And

        using (TextReader reader = new StreamReader(file))
        {
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
            return (T)xmlSerializer.Deserialize(reader);
        }

What will actually happen in the latter piece of code? Will the Dispose() be called?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T05:51:56+00:00Added an answer on May 25, 2026 at 5:51 am

    The resource of the using statement, reader will be disposed when the using scope ends. In you’re case that’s when the result of the deserialization has been casted to T.

    you could extend you’re code to the (roughly) equivalent below:

    TextReader reader = null;
    try{
      reader = new StreamReader(file);
      XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
      var obj = xmlSerializer.Deserialize(reader);
      T returnVal = (T)obj;
      return returnVal;
    } finally{
       reader.Dispose();
    }
    

    in that version it becomes clear that the last time reader is used is way before the return statement.

    If you were to return reader you would run into problems since the object returned would be disposed and hence unusable.

    EDIT:
    The IL of the above code is:

    IL_0000:  nop
      IL_0001:  ldnull
      IL_0002:  stloc.0
      .try
      {
        IL_0003:  nop
        IL_0004:  ldstr      ""
        IL_0009:  newobj     instance void [mscorlib]System.IO.StreamReader::.ctor(string)
        IL_000e:  stloc.0
        IL_000f:  ldtoken    !!T
        IL_0014:  call       class [mscorlib]System.Type [mscorlib]System.Type::GetTypeFromHandle(valuetype [mscorlib]System.RuntimeTypeHandle)
        IL_0019:  newobj     instance void [System.Xml]System.Xml.Serialization.XmlSerializer::.ctor(class [mscorlib]System.Type)
        IL_001e:  stloc.1
        IL_001f:  ldloc.1
        IL_0020:  ldloc.0
        IL_0021:  callvirt   instance object [System.Xml]System.Xml.Serialization.XmlSerializer::Deserialize(class [mscorlib]System.IO.TextReader)
        IL_0026:  stloc.2
        IL_0027:  ldloc.2
        IL_0028:  unbox.any  !!T
        IL_002d:  stloc.3
        IL_002e:  ldloc.3
        IL_002f:  stloc.s    CS$1$0000
        IL_0031:  leave.s    IL_003d
      }  // end .try
      finally
      {
        IL_0033:  nop
        IL_0034:  ldloc.0
        IL_0035:  callvirt   instance void [mscorlib]System.IO.TextReader::Dispose()
        IL_003a:  nop
        IL_003b:  nop
        IL_003c:  endfinally
      }  // end handler
      IL_003d:  nop
      IL_003e:  ldloc.s    CS$1$0000
      IL_0040:  ret
    } // end of method
    

    the thing to notice is that CS$1$0000 which is the return value is push to the stack just before the only ret instruction. So the order of execution is different from what it looks like in C# code. Further it’s worth noting the stloc.s CS$1$0000 and leave.s instrcutions which stores the return value followed by one of the glorified GOTOs. leave.s leaves the try and jumps to the label IL_003d, just before pushing the return value to the stack

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How to get the underlying type of an Observable collection using reflection? Consider following
Consider following: Partial View: <%= Html.ListBox(BlackList, Model.Select( x => new SelectListItem { Text =
Consider following script (it's total nonsense in pseudo-language): if (Request.hostMatch(asfasfasf.com) && someString.existsIn(new String[] {brr,
Consider following piece of code: JSONObject json = new JSONObject(); json.put(one, 1); json.put(two, 2);
Consider following code: ArrayList<Integer> aList = new ArrayList<Integer>(); aList.add(2134); aList.add(3423); aList.add(4234); aList.add(343); String tmpString
Consider following code: typedef SomeType type_t[2]; SomeType * arr1 = new type_t; //new or
Consider following code public class City { public string Name { get { return
Lets consider following two codes First: for (int i=0;i<10000000;i++) { char* tab = new
please consider following code #include <iostream> using namespace std; class Digit { private: int
Please let us consider following code: #include <iostream> using namespace std; union{ int i;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.