tryparse

What is better: int.TryParse or try { int.Parse() } catch [closed]

我们两清 提交于 2019-11-28 21:00:14
I know.. I know... Performance is not the main concern here, but just for curiosity, what is better? bool parsed = int.TryParse(string, out num); if (parsed) ... OR try { int.Parse(string); } catch () { do something... } Better is highly subjective. For instance, I personally prefer int.TryParse , since I most often don't care why the parsing fails, if it fails. However, int.Parse can (according to the documentation ) throw three different exceptions: the input is null the input is not in a valid format the input contains a number that procudes an overflow If you care about why it fails, then

DateTime.TryParse issue with dates of yyyy-dd-MM format

夙愿已清 提交于 2019-11-28 19:03:31
I have the following date in string format "2011-29-01 12:00 am" . Now I am trying to convert that to datetime format with the following code: DateTime.TryParse(dateTime, out dt); But I am alwayws getting dt as {1/1/0001 12:00:00 AM} , Can you please tell me why ? and how can I convert that string to date. EDIT: I just saw everybody mentioned to use format argument. I will mention now that I can't use the format parameter as I have some setting to select the custom dateformat what user wants, and based on that user is able to get the date in textbox in that format automatically via jQuery

string to double skips last decimal if it is zero?

。_饼干妹妹 提交于 2019-11-28 14:32:15
Here is my Problem. I need to Convert say "5.550" (string) to double as 5.550 that is double with 3 decimal digits. i have tried IFormatProvider while parsing but no use.it keeps skipping last zero(). Please advice. Thanks, Kumar M A double doesn't keep insignificant digits - there's no difference between 1.5 and 1.50000 as far as double is concerned. If you want to preserve insignificant digits, use decimal instead. It may well be more appropriate for you anyway, depending on your exact context. (We have very little context to go on here...) 来源: https://stackoverflow.com/questions/19574656

Dynamic TryParse for all data types

吃可爱长大的小学妹 提交于 2019-11-28 11:11:08
问题 I have the need to examine to see if an object can be converted to a specific DataType or not, and came up with this : public static bool TryParseAll(System.Type typeToConvert, object valueToConvert) { bool succeed = false; switch (typeToConvert.Name.ToUpper()) { case "DOUBLE": double d; succeed = double.TryParse(valueToConvert.ToString(), out d); break; case "DATETIME": DateTime dt; succeed = DateTime.TryParse(valueToConvert.ToString(), out dt); break; case "INT16": Int16 i16; succeed =

UInt32.TryParse() hex-number not working

陌路散爱 提交于 2019-11-28 10:57:10
For some reason the following C# Console program always outputs: 32 False wtf=0 What am I doing wrong? using System.Collections.Generic; using System.Linq; using System.Text; using System.Globalization; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Console.WriteLine(Convert.ToUInt32("0x20", 16)); UInt32 wtf = 0; Console.WriteLine(UInt32.TryParse("0x20", NumberStyles.HexNumber, // I've tried also AllowHexSpecifier CultureInfo.InvariantCulture, // I've also tried CurrentCulture out wtf)); Console.WriteLine("wtf={0}", wtf); } } } Peter You need to drop the "0x"

Culture invariant Decimal.TryParse()

£可爱£侵袭症+ 提交于 2019-11-28 08:04:43
I'm writing a custom string to decimal validator that needs to use Decimal.TryParse that ignores culture (i.e. doesn't care if the input contains "." or "," as decimal point separator). This is the suggested method: public static bool TryParse( string s, NumberStyles style, IFormatProvider provider, out decimal result ) I can't figure out what to use as the 3rd parameter. The examples I've seen look like this: culture = CultureInfo.CreateSpecificCulture("en-GB"); Decimal.TryParse(value, style, culture, out number) so they create a specific culture. CultureInfo does not have a

Enum.TryParse returns true for any numeric values

≡放荡痞女 提交于 2019-11-28 06:41:48
I'm running into a behavior I wasn't expecting when using Enum.TryParse. If I have an enum: public enum MyEnum { ValueA, ValueB, ValueC } And then I pass a numeric value (as a string) into Enum.TryParse, like: MyEnum outputEnum; bool result = Enum.TryParse("1234", out outputEnum); Despite the string "1234" not being a possible value, result will come back as true, and my outputEnum will have a value of 1234. Is there a way I can avoid this sort of behavior? I'm trying to write a function which will process arbitrary string input as an enum, and this has thrown a bit of a monkeywrench in my bad

Integer.TryParse - a better way?

大城市里の小女人 提交于 2019-11-28 05:18:23
I find myself often needing to use Integer.TryParse to test if a value is an integer. However, when you use TryParse, you have to pass a reference variable to the function, so I find myself always needing to create a blank integer to pass in. Usually it looks something like: Dim tempInt as Integer If Integer.TryParse(myInt, tempInt) Then I find this to be quite cumbersome considering that all I want is a simple True / False response. Is there a better way to approach this? Why isn't there an overloaded function where I can just pass the value I want to test and get a true / false response? No

Safely converting string to bool in PowerShell

时光怂恿深爱的人放手 提交于 2019-11-28 02:57:03
问题 I'm trying to convert an argument of my PowerShell script to a boolean value. This line [System.Convert]::ToBoolean($a) works fine as long as I use valid values such as "true" or "false", but when an invalid value, such as "bla" or "" is passed, an error is returned. I need something akin to TryParse, that would just set the value to false if the input value is invalid and return a boolean indicating conversion success or failure. For the record, I tried [boolean]::TryParse and [bool]:

Int32.TryParse() or (int?)command.ExecuteScalar()

纵然是瞬间 提交于 2019-11-28 02:37:13
问题 I have a SQL query which returns only one field - an ID of type INT. And I have to use it as integer in C# code. Which way is faster and uses less memory? int id; if(Int32.TryParse(command.ExecuteScalar().ToString(), out id)) { // use id } or int? id = (int?)command.ExecuteScalar(); if(id.HasValue) { // use id.Value } or int? id = command.ExecuteScalar() as int?; if(id.HasValue) { // use id.Value } 回答1: The difference between the three performance wise is negligible. The bottleneck is moving