[英]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.