[英]How do i run my Code without using Main method in C# - Doing a Luhn Algorithm
所以我一直在尝试制作一个 Luhn-Algorithm 技术上现在应该可以使用的算法,但是我在互联网上进行了一些搜索,没有人使用 Main 方法,所以我也没有,但是我的问题或者更像我的问题是我该如何运行没有主要方法的代码?? 我在哪里放置我的主要方法?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace LuhnAlgorithm
{
public static class Luhn
{
static bool IsValid(string number)
{
if (string.IsNullOrEmpty(number)) return false;
bool onSecondDigit = false;
int count = 0;
int sum = 0;
for (int i = number.Length - 1; i >= 0; i--)
{
char c = number[i];
if (!char.IsDigit(c))
{
if (c == ' ') continue;
return false;
}
int digit = c - '0';
if (onSecondDigit)
{
digit *= 2;
if (digit > 9) digit -= 9;
}
sum += digit;
count++;
onSecondDigit = !onSecondDigit;
}
return count > 1 && sum % 10 == 0;
}
}
}
你的应用程序总是需要一个入口点,它是你的程序 class 的 Main 方法。 从 C#9 开始,您可以在控制台应用程序中使用顶级语句,其中 C# 将为您添加 class 和 Main 方法。 所以你不必实现它,但从技术上讲它是存在的。
请参阅https://docs.microsoft.com/en-us/dotnet/csharp/fundamentals/program-structure/top-level-statements
您刚刚实现了一个 class,大多数代码示例并没有做更多的事情,因为您应该知道如何编写一个简单的 C# 应用程序,您可以在其中使用该 class。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.