要在C#中定义自定义方法,请使用以下语法-
<Access Specifier> <Return Type> <Method Name>(Parameter List) { Method Body }
若要调用自定义方法,请尝试运行以下代码。它具有checkPalindrome()
一种方法来检查二进制表示形式是否是回文式-
using System; public class Demo { public static long funcReverse(long num) { long myRev = 0; while (num > 0) { myRev <<= 1; if ((num & 1) == 1) myRev ^= 1; num >>= 1; } return myRev; } public static bool checkPalindrome(long num) { long myRev = funcReverse(num); return (num == myRev); } public static void Main() { //二进制值为5 us 101- long num = 5; if (checkPalindrome(num)) Console.WriteLine("Palindrome Number"); else Console.WriteLine("Not a Palindrome Number"); } }
输出结果
Palindrome Number