csharp判断数值奇偶性
时间: 2023-12-22 16:04:23 浏览: 33
以下是C#中判断数值奇偶性的方法:
1.使用取模运算符%判断
```csharp
int num = 5;
if(num % 2 == 0)
{
Console.WriteLine("偶数");
}
else
{
Console.WriteLine("奇数");
}
```
2.使用位运算符&判断
```csharp
int num = 5;
if((num & 1) == 0)
{
Console.WriteLine("偶数");
}
else
{
Console.WriteLine("奇数");
}
```
相关问题
csharp判断字符串为空
在 C# 中,可以使用以下方法来判断一个字符串是否为空:
1. 使用 `string.IsNullOrEmpty` 方法:
```csharp
string str = "Hello";
if (string.IsNullOrEmpty(str))
{
// 字符串为空或为 null
}
```
`string.IsNullOrEmpty` 方法用于判断字符串是否为空或为 null。如果字符串为空或为 null,返回 true;否则返回 false。
2. 使用 `string.IsNullOrWhiteSpace` 方法:
```csharp
string str = "Hello";
if (string.IsNullOrWhiteSpace(str))
{
// 字符串为空、为 null 或只包含空白字符
}
```
`string.IsNullOrWhiteSpace` 方法用于判断字符串是否为空、为 null 或只包含空白字符。如果字符串为空、为 null 或只包含空白字符,返回 true;否则返回 false。
请根据具体的需求选择适合的方法来判断字符串是否为空。
csharp 判断文件存不存在
可以使用System.IO命名空间下的File类中的Exists方法来判断文件是否存在。示例代码如下:
```csharp
using System.IO;
// 文件路径
string filePath = "D:/test.txt";
// 判断文件是否存在
if (File.Exists(filePath))
{
// 文件存在
Console.WriteLine("文件存在");
}
else
{
// 文件不存在
Console.WriteLine("文件不存在");
}
```
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)