Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
No Overload takes 5 arguments
Message
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Versions des environnements
Environment:
C# 4.0
OS:
Windows Server 2012
Network:
Windows 2008 Server
Database:
MS SQL Server
Application:
Web
Divers
Thread ID:
01583574
Message ID:
01583757
Vues:
34
>I'd agree that 'out' would be cumbersome with more than one (or possibly two) parameters. But how would you write a better version of TryParse() ?

Probably we will disagree in the definition of better :)

I try to avoid out and ref parameters as much as I can, not sure if it is a good thing or not, but it is who I am, in the case of TryParse I would return null if it was not successful and the code would be almost the same, take for, for example, the sample code from the help:
string[] dateStrings = {"05/01/2009 14:57:32.8", "2009-05-01 14:57:32.8", 
                        "2009-05-01T14:57:32.8375298-04:00", 
                        "5/01/2008 14:57:32.80 -07:00", 
                        "1 May 2008 2:57:32.8 PM", "16-05-2009 1:00:32 PM", 
                        "Fri, 15 May 2009 20:10:57 GMT" };
DateTime dateValue;

Console.WriteLine("Attempting to parse strings using {0} culture.", 
                  CultureInfo.CurrentCulture.Name);
foreach (string dateString in dateStrings)
{
   if (DateTime.TryParse(dateString, out dateValue)) 
      Console.WriteLine("  Converted '{0}' to {1} ({2}).", dateString, 
                        dateValue, dateValue.Kind);
   else
      Console.WriteLine("  Unable to parse '{0}'.", dateString);
}
Very simple to change, assuming that TryParse returns null for a failed attempt:
string[] dateStrings = {"05/01/2009 14:57:32.8", "2009-05-01 14:57:32.8", 
                        "2009-05-01T14:57:32.8375298-04:00", 
                        "5/01/2008 14:57:32.80 -07:00", 
                        "1 May 2008 2:57:32.8 PM", "16-05-2009 1:00:32 PM", 
                        "Fri, 15 May 2009 20:10:57 GMT" };
DateTime? dateValue;

Console.WriteLine("Attempting to parse strings using {0} culture.", 
                  CultureInfo.CurrentCulture.Name);

foreach (string dateString in dateStrings)
{  dateValue = (DateTime.TryParse(dateString) 
   if (dateValue != null)
      Console.WriteLine("  Converted '{0}' to {1} ({2}).", dateString, 
                        dateValue, dateValue.Kind);
   else
      Console.WriteLine("  Unable to parse '{0}'.", dateString);
}
So just a change in the declaration of dateValue and an extra line to do first the assignment and then the test instead of having all in one go, a little price to avoid the awful out :) (of course you might argue that null is even worse, but I never understood why it is treated with such distrust)
"The five senses obstruct or deform the apprehension of reality."
Jorge L. Borges?

"Premature optimization is the root of all evil in programming."
Donald Knuth, repeating C. A. R. Hoare

"To die for a religion is easier than to live it absolutely"
Jorge L. Borges
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform