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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:01:05+00:00 2026-06-05T21:01:05+00:00

I have a private static readonly field in my class: public class MyClass {

  • 0

I have a private static readonly field in my class:

public class MyClass
{
    // ISSUE #1 -- requires unproven: path != null
    private static readonly DirectoryInfo MyDirectory =
        new DirectoryInfo(Settings.Default.MyDirectoryPath);

    protected virtual void SomeMethod()
    {
        if (MyDirectory.Exists)
        {
            // ISSUE #2 -- requires unproven: !string.IsNullOrEmpty(path)
            var catalog = new DirectoryCatalog(MyDirectory.FullName);
        }
    }
}

For issue #1 I used a null coalescing operator to default to some magic string and that fixed it, but I don’t really like that solution. I was hoping there was a better solution.

For issue #2 the only thing I can think of is using a Contract.Assumes because if I attempt to use Contract.Requires(MyDirectory.Exists || !String.IsNullOrEmpty(MyDirectory.FullName)); it complains about visibility (private field used in a requires on a protected method).

  • 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-05T21:01:07+00:00Added an answer on June 5, 2026 at 9:01 pm

    Issue #1 is a result of Settings.Default.MyDirectoryPath being code generated by Visual Studio without any contracts on the property. This issue is not limited to null strings. Many API’s now have contracts that require say a TimeSpan to be non-negative but using a setting directly in the API will generate a Code Contracts warning.

    A way to solve this issue is to wrap the setting in a method that has a contract. E.g.:

    String GetMyDirectoryPath() {
      Contract.Ensures(Contract.Result<String>() != null);
      var myDirectoryPath = Settings.Default.MyDirectoryPath;
      Contract.Assume(myDirectoryPath != null);
      return myDirectoryPath;
    }
    

    Notice how the Contract.Assume really performs validation of your setting (which can’t be verified by Code Contracts because it is controlled by an external configuration file). Had it been a TimeSpan that is expected to be non-negative you can either use Contract.Assume to do the validation resulting in a ContractException or some other method using your own exception instead.

    Adding this extra layer is somewhat tedious but because the setting is defined outside the application it needs to be run-time validated at some point just as you have to validate interactive user input.

    Issue #2 is probably because DirectoryInfo doesn’t have any contracts defined. The easist way is to use Contract.Assume. This will make a statement about what you believe is the expected behavior of DirectoryInfo but a run-time check will still be in place to ensure that your belief is correct (provided that you keep the checks in your code).

    var path = MyDirectory.FullName;
    Contract.Assume(!string.IsNullOrEmpty(path));
    var catalog = new DirectoryCatalog(path);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class as following public class ScheduledUpdater { private static readonly object
We have this class here: public static class Helper { private static readonly Random
I have a class namespace LogToolsTest { public class Foo { private static readonly
I have this class: public static class CsvWriter { private static StreamWriter _writer =
public class MyClass<T> { public static readonly String MyStringValue; static MyClass() { MyStringValue =
I have the following helper class (simplified): public static class Cache { private static
I have a static class with static private readonly member that's set via the
If I have a boolean field like: private static final boolean DEBUG = false;
I have a class (singleton) and it contains a static Dictionary private static Dictionary<string,
I have a class Constants in which I store a number of static readonly

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.