hi
I want to write a programme that the string just get the numbers Not characters (property) with C# in Console. can anybody help me plz?
Property in C#
1 Answer
Know the answer? Post it. Somebody with the same question will find it here.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
VulpesRank #55 · 29.2K reputationPosted Jul 24, 2011, 1:55 AM EDT
using System;
class MyClass
{
string numericString = "0"; // default value
public string NumericString
{
get { return numericString; }
set
{
double number;
bool isValid = double.TryParse(value, out number);
if (isValid)
numericString = number.ToString();
else
throw new ArgumentException(String.Format("'{0}' is not a numeric string", value));
}
}
}
class Program
{
static void Main()
{
MyClass mc = new MyClass();
try
{
mc.NumericString = "12.34";
mc.NumericString = "abc";
}
catch(ArgumentException ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadKey();
}
}