How to call custom methods in C#?


To define a custom method in C#, use the following syntax −

<Access Specifier> <Return Type> <Method Name>(Parameter List) {
   Method Body
}

To call a custom method, try to run the following code. It has the checkPalindrome() method which is called to checker whether the binary representation is Palindrome or not −

Example

 Live Demo

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() {
      // Binary value of 5 us 101
      long num = 5;

      if (checkPalindrome(num))
      Console.WriteLine("Palindrome Number");
      else
      Console.WriteLine("Not a Palindrome Number");
   }
}

Output

Palindrome Number

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 20-Jun-2020

230 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements