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 8103977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:42:20+00:00 2026-06-05T23:42:20+00:00

I have a small utility class : public static class SerializationUtilities { public static

  • 0

I have a small utility class :

public static class SerializationUtilities
{
    public static string ToXml<T>(T @object)
    {
        Contract.Requires<ArgumentNullException>(@object != null);
        Contract.Requires<ArgumentException>(typeof(T).IsSerializable);
        Contract.Ensures(!string.IsNullOrEmpty(Contract.Result<string>()));

        var xs = new XmlSerializer(typeof(T));
        var result = new StringBuilder();

        using (var sw = new StringWriter(result))
        {
            xs.Serialize(sw, @object);
        }
        return result.ToString();
    }

    public static T FromXml<T>(string xml)
    {
        Contract.Requires<ArgumentNullException>(!string.IsNullOrEmpty(xml));
        Contract.Ensures(!object.Equals(Contract.Result<T>(), default(T)));

        var xs = new XmlSerializer(typeof(T));
        using (var sr = new StringReader(xml))
        {
            return (T)xs.Deserialize(sr);
        }
    }
}

The two methods are working as expected.

However, the code contract static checker is throwing two warnings :

Warning 30 CodeContracts: ensures unproven:
!string.IsNullOrEmpty(Contract.Result())

Warning 28 CodeContracts: ensures unproven:
!object.Equals(Contract.Result(), default(T))

Why are these warning emitted ? What is the correct (best?) way to contractualize my methods ?

  • 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-06-05T23:42:21+00:00Added an answer on June 5, 2026 at 11:42 pm

    Your Contract.Ensures() can’t be so strict.

    Most of framework methods aren’t decorated with contracts. You can’t be sure that StringBuilder.ToString() won’t return an empty string because there is not information about what XmlSerializer.Serialize() will do. Same for deserializing: no informations to assert that XmlSerializer.Deserialize() won’t return a default(T) object.

    As pointed out by @TrustMe-ImADoctor in the comment you may have to add additional checks in your code for that assertion, static analysis will then see that the case is handled and impossible. I guess you do not need to pollute your code with more checks, for this interop cases you may just use Contract.Assume().

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

Sidebar

Related Questions

I've created a small utility function for string conversion so that I don't have
I have a small utility extension method that performs some null checks on some
I have written a small utility to collect system data and output it to
I have small class called 'Call' and I need to store these calls into
Assume I have a class a: class a { public: void load_data( ); private:
We have a small utility that finds unused items on our server and zips
I have just written a small utility to be used in a pre-build event.
I have a small utility script written as .Net/C# console app purely for the
I have a small command line utility project that I'm using Maven to manage.
I have a requirment to build a small utility, which fires a sql query

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.