C#布尔方法帮助! [英] C# boolean method help!
问题描述
我一直在编写一个项目的代码,我想知道如何创建一个方法(IsEnrolled)来返回一个布尔值(需要使用if语句。)这个方法必须返回一个真实的语句,学生是注册的,如果学生没有注册,则为假。问题是我的CurrentEnrolledCourse属性是一个字符串,当我创建它时,我不断收到一条错误,上面写着Can not Implicity转换类型'字符串'为'bool'
这是我到目前为止的代码(这只是代码的一部分而不是完整的代码!)
public bool IsEnrolled()
{
if (CurrentEnrolledCourse = null )
{
return false ;
}
else
{返回 true ;
}
}
我尝试了什么:
我试图将字符串转换为bool但是我发现了什么它呢!
显然你需要更熟悉C#语法; 特别是分配和比较运算符。
public bool IsEnrolled()
{
if (CurrentEnrolledCourse == null )
{
return false ;
}
其他
{
返回 真跨度>;
}
}
假设学生只有当CurrentEnrolledCourse不为空时才注册。
更好的方法(因为需要使用if语句):
public bool IsEnrolled()
{
if (CurrentEnrolledCourse == null )
return false ;
return true ;
}
正确的方式:
public bool IsEnrolled()
{
return !string。 IsNullOrEmpty(CurrentEnrolledCourse);
}
(它应该是只获取属性。)
< blockquote>将
CurrentEnrolledCourse
与null进行比较时输入
==
。
试试这个:
public bool IsEnrolled()
{
if ( string .IsNullOrEmpty(CurrentEnrolledCourse))
{
return false ;
}
其他
{
返回 真跨度>;
}
}
或只需修改你的代码:
public bool IsEnrolled()
{
if (CurrentEnrolledCourse == null )
{
< span class =code-keyword> return false ;
}
else
{ return 真跨度>;
}
}
谢谢,
它应该是
if (CurrentEnrolledCourse == null ) ...
此外,所有那些if-else都是毫无意义的。你有没有想过这个?您在if下的表达式已经是布尔值,为什么if?这样做:
public bool IsEnrolled(){ return CurrentEnrolledCourse!= null ; }
不是那么简单吗? :-)
现在,一点奖金:
让我来告诉你一个老程序员的伎俩:而不是if(CurrentEnrolledCourse == null)
写if(null == CurrentEnrolledCourse)
。但为什么?两个片段的含义和功能完全相同。所以为什么?考虑一下。让我们说,你再次错误地输入=(赋值)而不是==(相等的比较)。但在第二种形式,编译器会给你一个明确的错误信息,这将帮助你立即发现这个问题;这是因为你真的可以为变量/成员分配null而不能为null赋值,即所谓的立即常量。
-SA
Ive been writing a code for a project and I was wondering how to create a method (IsEnrolled) to return a Boolean value (need to use if statement.) This method will have to return a true statement is student is enrolled and a false if student isn't enrolled. The problem is my CurrentEnrolledCourse property is a string and when I create it I keep getting an error that says "Cannot Implicity convert type 'string' to 'bool'
Here is the code I have so far (This is just part of the code not the complete code!)
public bool IsEnrolled()
{
if (CurrentEnrolledCourse = null)
{
return false;
}
else
{ return true;
}
}
What I have tried:
I have tried to convert string to bool but I havnt found anything on it yet!
This is clearly homework since "need to use if statement".
Clearly you need to get more familiar with C# syntax; specifically the assignment and comparison operators.
public bool IsEnrolled() { if (CurrentEnrolledCourse == null) { return false; } else { return true; } }
Assuming the student is enrolled only if the CurrentEnrolledCourse isn't empty.
A better way (since "need to use if statement"):
public bool IsEnrolled() { if (CurrentEnrolledCourse == null) return false; return true; }
The right way:
public bool IsEnrolled() { return !string.IsNullOrEmpty(CurrentEnrolledCourse); }
(And it should be a "get-only" property.)
Put "==" while comparing
CurrentEnrolledCoursewith null.
Try this:
public bool IsEnrolled() { if (string.IsNullOrEmpty(CurrentEnrolledCourse)) { return false; } else { return true; } }
or just modify your code:
public bool IsEnrolled() { if (CurrentEnrolledCourse == null) { return false; } else { return true; } }
Thanks,
It should be
if (CurrentEnrolledCourse == null) ...
Moreover, all those "if-else" are just pointless. Did you even think of that? Your expression under "if" is already a Boolean, so why "if"? Do just this:
public bool IsEnrolled() { return CurrentEnrolledCourse != null; }
Isn't that simple? :-)
Now, a little bonus:
Let me tell you one old programmer's trick: instead ofif (CurrentEnrolledCourse == null)
writeif (null == CurrentEnrolledCourse)
. But why? The meaning and functionality of both fragments is exactly the same. So why? Just think about it. Let's say, you again mistakenly typed "=" (assignment) instead of "==" (comparison for equality). But in the second form, the compiler will give you a clear error message which will help you to spot this problem immediately; and this is because you can really assign null to a variable/member and cannot assign anything to the null, which is the so-called immediate constant.
—SA
这篇关于C#布尔方法帮助!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!