Tutorialspoint

C# TrimEnd()

using System;
class Program {
   static void Main() {
	
      String str ="7341111".TrimEnd(new Char[] { '1' } );
      Console.WriteLine(str);
   }
}

C# TrimStart()

using System;
class Program {
   static void Main() {
	
      String str ="0009678".TrimStart(new Char[] { '0' } );
      Console.WriteLine(str);
   }
}

C# ReadLine() methods

using System;
class Program {
   static void Main() {
        
      int x = 10;
      Console.WriteLine(x);
      Console.Write("\nPress any key to continue... ");
      Console.ReadLine();
        
   }
}

C# Infinity or Exception divide by 0

using System;
namespace ErrorHandlingApplication {
   class DivNumbers {
      int result;
      
      DivNumbers() {
         result = 0;
      }
      public void division(int num1, int num2) {
         try {
            result = num1 / num2;
         } catch (DivideByZeroException e) {
            Console.WriteLine("Exception caught: {0}", e);
         } finally {
            Console.WriteLine("Result: {0}", result);
         }
      }
      static void Main(string[] args) {
         DivNumbers d = new DivNumbers();
         d.division(25, 0);
         Console.ReadKey();
      }
   }
}

C# ReadLine()

using System;
class Program {
   static void Main() {
        
      int x = 10;
      Console.WriteLine(x);
      Console.Write("\nPress any key to continue... ");
      Console.ReadLine();   
   }
}

C# String Join() method

using System;

namespace StringApplication {

   class StringProg {
   
      static void Main(string[] args) {
         string[] starray = new string[]{"Down the way nights are dark",
            "And the sun shines daily on the mountain top",
            "I took a trip on a sailing ship",
            "And when I reached Jamaica",
            "I made a stop"};

         string str = String.Join("\n", starray);
         Console.WriteLine(str);
      }
   }
}

C# decrement operator

using System;
class Program {
   static void Main() {
	
      int a, b;
      a = 10;
      Console.WriteLine(--a);
      Console.WriteLine(a--);
        
      b = a;
      Console.WriteLine(a);
      Console.WriteLine(b);
   }
}

C# Abort

using System;
using System.Threading;

namespace MultithreadingApplication {
   class ThreadCreationProgram {
      public static void CallToChildThread() {
         try {
            Console.WriteLine("Child thread starts");
            
            // do some work, like counting to 10
            for (int counter = 0; counter <= 10; counter++) {
               Thread.Sleep(500);
               Console.WriteLine(counter);
            }
            
            Console.WriteLine("Child Thread Completed");
         } catch (ThreadAbortException e) {
            Console.WriteLine("Thread Abort Exception");
         } finally {
            Console.WriteLine("Couldn't catch the Thread Exception");
         }
      }
      static void Main(string[] args) {
         ThreadStart childref = new ThreadStart(CallToChildThread);
         Console.WriteLine("In Main: Creating the Child thread");
         
         Thread childThread = new Thread(childref);
         childThread.Start();
         
         //stop the main thread for some time
         Thread.Sleep(5000);
         
         //now abort the child
         Console.WriteLine("In Main: Aborting the Child thread");
         
         childThread.Abort();
         Console.ReadKey();
      }
   }
}

C# two-dimensional array

using System;

namespace Demo {
   class Program {
      static void Main(string[] args) {

         string[,] a = new string[2, 2] {{"abc","def"}, {"dgh","yui"} };
         int i, j;
         
         for (i = 0; i < 2; i++) {
            
            for (j = 0; j < 2; j++) {
               Console.WriteLine("a[{0},{1}] = {2}", i, j, a[i,j]);
            }
         }
         Console.ReadKey();
      }
   }
}

C# jagged string array

using System;

namespace Demo {
   class Program {
      static void Main(string[] args) {
         
         string[][] a = new string[][]{new string[]{"abc","jjj"},new string[]{"frdf","rrt"} };
         int i, j;
         
         for (i = 0; i < 2; i++) {
            for (j = 0; j < 2; j++) {
               Console.WriteLine("a[{0}][{1}] = {2}", i, j, a[i][j]);
            }
         }
         Console.ReadKey();
      }
   }
}

1 2 3 4 5 6 7 ... 46 Next
Advertisements
Loading...