using systeme c# code example

Example: c# system classes

// ----------------- LIST OF SOME SYSTEM CLASSES ---------------------//

// ______Class StringBuilder______ 
// Creates an object that represents a mutable string of characters
using System.Text;

StringBuilder myText = new StringBuilder();
myText.Append("Hello World");
myText.Append('\n');

Console.WriteLine(myText);



// ______Class ArrayList______ 
// Creates a mutable array of objects of different types (an array whose
// size is dynamically increased as required.
using System.Collections;

ArrayList myArray = new ArrayList();
myArray.Add("Hello");
myArray.Add(12);
myArray.Add(true);
			
Console.WriteLine(myArray[0]);
//OR
foreach(object obj in myArray){
	Console.WriteLine(obj);
}



// ______Class Timer______ 
// Provides a mechanism for executing a method on a thread pool thread 
// at specified intervals
using System.Threading;

class TimerClock
{
  
  Timer myTimer;

  public void StartingTimer()
  {
      myTimer = new Timer(TimerCallbackFuntion, null, 0, 1000); // Start now (0) with a period of 1 second between each execution of the callback function  
      // ... do some code ...
      Console.ReadKey(); // This will stop the main thread from ending too quickly!
  }

  private void TimerCallbackFuntion(Object obj)
  {
     Console.WriteLine("This is a message that repeats every second until the main thread terminates or StopTimer() is called")
     GC.Collect();  // Garbage Collector
  }

  public void StopTimer() 
  {
	 myTimer.Dispose();	
     Console.WriteLine("The timer was stopped!");
  }
 
} 



// ______Class Math______ 
// Provides constants and static methods for trigonometric, logarithmic, 
// and other common mathematical functions.
using System;

Console.WriteLine( Math.Min(6, 14 ); // returns 6
Console.WriteLine( Math.Pow(2, 5) ); // returns 2^5 = 32
Console.WriteLine( Math.PI );  // returns the value of Pi
Console.WriteLine( Math.sqrt(4) );  // returns the square root of 4
Console.WriteLine( Math.abs(-3) );  // returns 3      
Console.WriteLine( Math.cos(1) );  // returns 0.5
// ...
  
  
                  
// ______Class Random______ 
// Represents a pseudo-random number generator.                
using System;
                  
var rand = new Random();   
                  
Console.WriteLine("Five random integers between 0 and 100:");
                  
for (int ctr = 0; ctr <= 4; ctr++)
    Console.Write("{0,8:N0}", rand.Next(101));
                
Console.WriteLine("Five random integers between 50 and 100:");
                  
for (int ctr = 0; ctr <= 4; ctr++)
    Console.Write("{0,8:N0}", rand.Next(50, 101));   
                  
Console.WriteLine("Display 5 random floating point values from 0 to 1..");
                  
for (int ctr = 0; ctr <= 4; ctr++)
    Console.Write("{0,8:N3}", rand.NextDouble());                  
                  
//...
                  

                  
// ______Class Regex______ 
// Represents an immutable regular expression. We use a regular 
// expression to check for repeated occurrences of words in a string.                   
using System.Text.RegularExpressions;          
                  

Regex rx = new Regex(@"\b(?<word>\w+)\s+(\k<word>)\b");   // Define a regular expression for repeated words like "the the", "dog dog",...

string text = "the the quick brown fox  fox jumps over the lazy dog dog."; // Define a test string.

MatchCollection matches = rx.Matches(text);   // Find matches.

        
Console.WriteLine("{0} matches found in:\n -> {1}", matches.Count, text);  // Report the number of matches to the pattern that were found.

foreach (Match match in matches)   // Report on each match.
{
   GroupCollection groups = match.Groups;
   Console.WriteLine("'{0}' repeated at positions {1} and {2}",     //result: 'the' repeated at positions 0 and 4
                              groups["word"].Value,                 //        'fox' repeated at positions 20 and 25
                              groups[0].Index,                      //        'dog' repeated at positions 49 and 53
                              groups[1].Index);
}