❔ ✅ How can I make a variable any type?
How can I make it so a variable has a type 'Any'?
30 Replies
You could make it an
object?
(The ? is part of the type)
You’d have to cast it whenever you wanted to get at the actual valueIf i were to add two vars with the type
object?
would it work?
just curiousSimple: you don't
i'll just have to go with
object?
for now thenWhy do you think you want this?
No, as the type
object?
doesn’t have a + operator definedBecause there may very well be a better alternative.
you know any?
ah i see
Give context to what your doing
What are you trying to do?
tryna accept input from the user but i dont know the type that user will be entering, and i want to perform actions on number given by the user
If you accept user input it will always be a string
type as in i know it'll not be a string
At least using
Console.ReadLine
As in from Console.ReadLine? That returns a string
i did, i also tried
Convert.ToInt32(Console.ReadLine());
but that is strict to int
not any datatypeyes...?
Console.ReadLine()
returns a string.
You can parse that string into other data types (ex. numbers), but you still have to know its type.
There is no way to just say "figure out what this string means"
You have to be strict and specify explicitly what you expect it to be.is that always the case?
Yes.
Even if you use
object
here, you won't get around this.we always will have to know the type of data?
A string is a string. An int is an int.
Yes
alr ty
As mentioned, C# is a statically typed language.
Types always have to be known.
no wonder
now i know
can i close this post or something now?
ye
/close
alr
Something you can do is check to see if the string only contains numeric chars, and if so convert. Aka int32.TryParse() https://learn.microsoft.com/en-us/dotnet/api/system.int32.tryparse?view=net-7.0#system-int32-tryparse(system-string-system-int32@)
Int32.TryParse Method (System)
Converts the string representation of a number to its 32-bit signed integer equivalent. A return value indicates whether the operation succeeded.
Better avoid
Convert
, because $tryparseThe TryParse pattern is considered best practice of parsing data from a string:
- a TryParse method returns
true
or false
to inform you if it succeeded or not, so you can use it directly in a condition,
- since C# 7 you can declare a variable that will be used as an out
argument inline in an argument list,
- it forces you to check if the out
argument contains valid data afterwards,
Avoid: Convert.ToInt32 — it's a bad choice for parsing an int
. It exists only for backwards compatibility reasons and should be considered last resort. (Note: Convert does contain useful conversion methods: To/FromBase64String
, To/FromHexString
, ToString(X value, int toBase)
, ToX(string? value, int fromBase)
)
Avoid: int.Parse — you have to use a try
/catch
statement to handle invalid input, which is a less clean solution.
Use int.TryParse https://docs.microsoft.com/en-us/dotnet/api/system.int32.tryparse?view=net-5.0#System_Int32_TryParse_System_String_System_Int32__ Int32.TryParse Method (System)
Converts the string representation of a number to its 32-bit signed integer equivalent. A return value indicates whether the operation succeeded.
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.