---
title: "Write another function for this code using C#"  
description: "Write another function for this code using C#"  
author: "E E Cummings"  
published: 2013-10-05  
updated: 2013-10-05  
canonical: https://www.mindstick.com/forum/1633/write-another-function-for-this-code-using-c-sharp  
category: "c#"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# Write another function for this code using C#

```
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string numberIn;
            int numberOut;

            numberIn = Console.ReadLine();

            while (!int.TryParse(numberIn, out numberOut) || numberOut < 0)
            {
                Console.WriteLine("Invalid. Enter a number that's 0 or higher.");
                numberIn = Console.ReadLine();
            }
        }
    }
}
```

## Replies

### Reply by Dag Hammarskjold

```
static void Main(string[] args)
    {
        string numberIn = Console.ReadLine();
        int numberOut;
        while(!IsNumeric(numberIn))
        {
            Console.WriteLine("Invalid.  Enter a number that's 0 or higher.");
            numberIn = Console.ReadLine();
        }
        numberOut = int.Parse(numberIn);
    }
    private static bool IsNumeric(string num)
    {
        return num.ToCharArray().Where(x => !Char.IsDigit(x)).Count() == 0;
    }
```


---

Original Source: https://www.mindstick.com/forum/1633/write-another-function-for-this-code-using-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
