# Help needed with C#

**URL:** <https://forum.codewithmosh.com/t/help-needed-with-c/14484>\
**Category:** C#\
**Created:** [August 17, 2022, 11:41am UTC](https://forum.codewithmosh.com/t/help-needed-with-c/14484 "2022-08-17T11:41:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mr\_Walters](https://avatars.discourse-cdn.com/v4/letter/m/6bbea6/32.png) [@Mr\_Walters](https://forum.codewithmosh.com/u/Mr_Walters)\
**Post date:** [August 17, 2022, 11:41am UTC](https://forum.codewithmosh.com/t/help-needed-with-c/14484/1 "2022-08-17T11:41:40Z")

</div>

Hey, I just started learning c# and I wanted to know if there is a way to check if a user enters the right thing.

For e.g. The program asks for a string but an integer is enter. How would I go about checking the validity of the input?

---

<div class="post-metadata">

**Author:** ![UniqueNospaceShort](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/uniquenospaceshort/32/1814_2.png) [@UniqueNospaceShort](https://forum.codewithmosh.com/u/UniqueNospaceShort)\
**Post date:** [August 17, 2022, 3:50pm UTC](https://forum.codewithmosh.com/t/help-needed-with-c/14484/2 "2022-08-17T15:50:48Z")

</div>

C# is strongly typed so you should give a try at parsing the data. There are objects in C# that represent primitive types. Actually their name correspond the the .NET types. For instance `int` is `Int32`. Those objects have methods such as `Parse` or `TryParse`. There is also a `Convert` object.

Here some docs:

[Convert](https://docs.microsoft.com/en-us/dotnet/api/system.convert)  
[Int32](https://docs.microsoft.com/en-us/dotnet/api/system.int32)

---

<div class="post-metadata">

**Author:** ![jmrunkle](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/jmrunkle/32/3765_2.png) [@jmrunkle](https://forum.codewithmosh.com/u/jmrunkle)\
**Post date:** [August 17, 2022, 4:01pm UTC](https://forum.codewithmosh.com/t/help-needed-with-c/14484/3 "2022-08-17T16:01:28Z")

</div>

The case of an integer being entered when you wanted a “string” is a tricky one because you can have strings like “1”. You may need to use a regex library to do further data validation (eg. to make sure the string only contains alphabetic characters and whitespace).

---

<div class="post-metadata">

**Author:** ![UniqueNospaceShort](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/uniquenospaceshort/32/1814_2.png) [@UniqueNospaceShort](https://forum.codewithmosh.com/u/UniqueNospaceShort)\
**Post date:** [August 17, 2022, 4:21pm UTC](https://forum.codewithmosh.com/t/help-needed-with-c/14484/4 "2022-08-17T16:21:15Z")

</div>

Oh man reading again I realise I completely did not understand the question. Sorry for that.

Depends on application type. I don’t know how to validate data outside of ASP. It is explained in the ASP course should you follow it.  
I did not do such thing in XAML based applications such as WPF or Xamarin /.NET MAUI but there seem to be a way around that.

[Validation in XAML based apps](https://docs.microsoft.com/en-us/dotnet/desktop/wpf/data/how-to-implement-binding-validation?view=netframeworkdesktop-4.8&viewFallbackFrom=netdesktop-6.0)

---

<div class="post-metadata">

**Author:** ![Mr\_Walters](https://avatars.discourse-cdn.com/v4/letter/m/6bbea6/32.png) [@Mr\_Walters](https://forum.codewithmosh.com/u/Mr_Walters)\
**Post date:** [September 3, 2022, 1:47am UTC](https://forum.codewithmosh.com/t/help-needed-with-c/14484/5 "2022-09-03T01:47:27Z")

</div>

I tried using the int.parse and it worked. Thanks guys.
