0

I like to occasionally have a look at .NET libraries and glean some insight as to how I should be doing things. I mean those guys are the experts and I'm always ready to learn. But I'm a little stumped on these two methods:

[ResourceExposure(ResourceScope.Machine)]
[ResourceConsumption(ResourceScope.Machine)] 
private static String[] InternalReadAllLines(String path, Encoding encoding)
{
    Contract.Requires(path != null);
    Contract.Requires(encoding != null); 
    Contract.Requires(path.Length != 0); 
    .
    .
    .
}



[System.Security.SecuritySafeCritical] 
[ResourceExposure(ResourceScope.Machine)]
[ResourceConsumption(ResourceScope.Machine)] 
public static IEnumerable<String> ReadLines(String path, Encoding encoding) 
{
    if (path == null) 
        throw new ArgumentNullException("path");
    if (encoding == null)
        throw new ArgumentNullException("encoding");
    if (path.Length == 0) 
        throw new ArgumentException(Environment.GetResourceString("Argument_EmptyPath"));
    Contract.EndContractBlock();  

    .
    .
    .   
}

So I see this sweet way to ensure parameters are set Contract.Requires(...), really concise and clean. Then another method has a stack of if statements followed by a Contract.EndContractBlock(). Doesn't a BLOCK imply that there's a starting point?

Anyone know if the difference is by design for some specific reason or just different coding standard by different developer at different points in time?

NOTE: Whilst this is along the same lines as another question it's not an exact duplicate.

Stephen York
  • 1,247
  • 1
  • 13
  • 42
  • See https://stackoverflow.com/questions/8448745/net-code-contracts-where-to-learn-more – MarcinJuraszek Dec 05 '17 at 23:22
  • "Doesn't a BLOCK imply that there's a starting point?" - the method start counts as an implicit starting point, since contracts are checked *first*; but ... yeah, see the link – Marc Gravell Dec 05 '17 at 23:28
  • Thanks @MarcinJuraszek, but why use it in one place but not another? Also when using Contract.Requires, is there no need to specify an end? – Stephen York Dec 05 '17 at 23:28
  • Public and private methods do require different behaviors. I believe that's why the two methods look differently. You might dig further into the recommendations on code contracts, and I think you should be able to locate the necessary chapters somewhere. – Lex Li Dec 05 '17 at 23:56

0 Answers0