Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Best way to re-factor
Message
De
15/02/2013 12:19:49
 
 
À
15/02/2013 07:19:17
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Versions des environnements
Environment:
C# 4.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Divers
Thread ID:
01565967
Message ID:
01566149
Vues:
62
>>>
>>>Personally I'm fine with TryXXX() methods - the reason they are there is to avoid an exception ( try/catch)
>>
>>Presumably the TryParse() method is actually implemented as a Parse() within a try/catch anyway. So not more efficient - just easier to use......
>
>Didn't think that
>
>but to put my mind at rest - had to figure that one out
>
>bool Viv.TryDecide
>{
> compare Number.StringToNumber() and Number.TryStringToNumber()
>}
>
>
>
>
>(1) Int32.Parse
>
>        public static int Parse(String s) {
>            return Number.ParseInt32(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo); 
>        }
>
>
>Number.ParseInt32()
>
>       internal unsafe static Int32 ParseInt32(String s, NumberStyles style, NumberFormatInfo info) {
>
>            Byte * numberBufferBytes = stackalloc Byte[NumberBuffer.NumberBufferBytes];
>            NumberBuffer number = new NumberBuffer(numberBufferBytes); 
>            Int32 i = 0;
> 
>            StringToNumber(s, style, ref number, info, false); 
>
>            if ((style & NumberStyles.AllowHexSpecifier) != 0) { 
>                if (!HexNumberToInt32(ref number, ref i)) {
>                    throw new OverflowException(Environment.GetResourceString("Overflow_Int32"));
>                }
>            } 
>            else {
>                if (!NumberToInt32(ref number, ref i)) { 
>                    throw new OverflowException(Environment.GetResourceString("Overflow_Int32")); 
>                }
>            } 
>            return i;
>        }
>
>Number.StringToNumber
>
>      private unsafe static void StringToNumber(String str, NumberStyles options, ref NumberBuffer number, NumberFormatInfo info, Boolean parseDecimal) { 
>
>            if (str == null) {
>                throw new ArgumentNullException("String");
>            } 
>            Contract.EndContractBlock();
>            Contract.Assert(info != null, ""); 
>            fixed (char* stringPointer = str) { 
>                char * p = stringPointer;
>                if (!ParseNumber(ref p, options, ref number, null, info , parseDecimal) 
>                    || (p - stringPointer < str.Length && !TrailingZeros(str, (int)(p - stringPointer)))) {
>                    throw new FormatException(Environment.GetResourceString("Format_InvalidString"));
>                }
>            } 
>        }
>
>
>(2)
>Int32.TryParse
>
>       public static bool TryParse(String s, out Int32 result) { 
>            return Number.TryParseInt32(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result); 
>        }
>
>
>Number.TryParseInt32
>
>       internal unsafe static Boolean TryParseDouble(String value, NumberStyles options, NumberFormatInfo numfmt, out Double result) {
>            Byte * numberBufferBytes = stackalloc Byte[NumberBuffer.NumberBufferBytes]; 
>            NumberBuffer number = new NumberBuffer(numberBufferBytes);
>            result = 0;
>
> 
>            if (!TryStringToNumber(value, options, ref number, numfmt, false)) {
>                return false; 
>            } 
>            if (!NumberBufferToDouble(number.PackForNative(), ref result)) {
>                return false; 
>            }
>            return true;
>        }
>
>
>Number.TryStringToNumber()
>
>      internal unsafe static Boolean TryStringToNumber(String str, NumberStyles options, ref NumberBuffer number, StringBuilder sb, NumberFormatInfo numfmt, Boolean parseDecimal) {
> 
>            if (str == null) {
>                return false; 
>            } 
>            Contract.Assert(numfmt != null, "");
> 
>            fixed (char* stringPointer = str) {
>                char * p = stringPointer;
>                if (!ParseNumber(ref p, options, ref number, sb, numfmt, parseDecimal)
>                    || (p - stringPointer < str.Length && !TrailingZeros(str, (int)(p - stringPointer)))) { 
>                    return false;
>                } 
>            } 
>
>            return true; 
>        }
>
Interesting - so Parse() throws exceptions all the way back up the chain - two in StringToNumber and two more in Int32 ParseInt32 ; TryParse() has none.
I prefer TryParse() evne more than I did before :=}
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform