That's some odd code: You are passing a string into the method but immediately overwriting it. You can't make the new string available to the method that calls IsDualNumber because strings are immutable (which means that they can't be changed - a new string is generated each time you try) and because what you pass is a reference to the string rather than the string content. So passing a string in is pointless!
Instead, I'd read the string in the calling method, and pass that to the IsDualNumber method.
Then, you have the next problem - which is that a string is a Collection of
char
values. So when you use an index, to retrieve a value it returns a single char - and '1' is not the same as 1, '0' is not the same as 0.
Your method always returns a
false
- so it's not really a bool at all!
I'd do it differently:
string s = Console.ReadLine();
if (IsDualNumber(s))
{
Console.WriteLine("The number \"{0}\" is a boolean", s);
}
else
{
Console.WriteLine("\"{0}\" is not a boolean", s);
}
...
public static bool IsDualNumber(string s)
{
foreach (char c in s)
{
if(c != '1' && c != '0')
{
return false;
}
}
return true;
}