Tutorialspoint

C# list by using an index

using System;
using System.Collections.Generic;
using System.Linq;

class Program {
   static void Main() {
      List<string> list1 = new List<string>() {
         "Hanks",
         "Lawrence",
         "Beckham",
         "Cooper",
      };
        
      Console.Write("Initial list...\n");
      foreach (string list in list1) {
         Console.WriteLine(list);
      }
        
      Console.Write("Removing element from the list...\n");
      list1.RemoveAt(1);

      foreach (string list in list1) {
         Console.WriteLine(list);
      }
   }
}

C# convert a list of characters unto a string

using System;

class Program {
   static void Main() {
      char[] arr = new char[5];
      arr[0] = 'Y';
      arr[1] = 'E';
      arr[2] = 'S';

      // converting to string
      string res = new string(arr);

      Console.WriteLine(res);
   }
}

C# read a line from the console

using System;
using System.Collections.Generic;

class Demo {
   static void Main() {
      string str;
        
      // use ReadLine() to read the entered line
      str = Console.ReadLine();
		
      // display the line
      Console.WriteLine("Input = {0}", str);
   }
}

C# Lambda Expressions

using System;
using System.Collections.Generic;

class Demo {
    static void Main() {
        List<int> list = new List<int> { 44, 6, 34, 23, 78 };

        int res = list.FindIndex(x => x > 50);
        Console.WriteLine("Index: "+res);
    }
}

C# Pattern matching

using System;
using System.Text.RegularExpressions;

namespace Demo {
   class Program {
      private static void showMatch(string text, string expr) {
         Console.WriteLine("The Expression: " + expr);
         MatchCollection mc = Regex.Matches(text, expr);
         
         foreach (Match m in mc) {
            Console.WriteLine(m);
         }
      }
      static void Main(string[] args) {
         string str = "Mandatory requirements for a Cricketer Match Event!";
         
         Console.WriteLine("Matching words that start with 'M': ");
         showMatch(str, @"\bM\S*");
         Console.ReadKey();
      }
   }
}

C# Multithreading

using System;
using System.Threading;

namespace Demo {
   class Program {
      static void Main(string[] args) {
         Thread th = Thread.CurrentThread;
         th.Name = "MainThread";
         
         Console.WriteLine("This is {0}", th.Name);
         Console.ReadKey();
      }
   }
}

C# Passing arrays to methods

using System;

namespace Test {
   class Demo {
      int displaySum(int[] arr, int size) {
         int i;
         int sum = 0;
         
         for (i = 0; i < size; ++i) {
            sum += arr[i];
         }
         
         return sum;
      }
      static void Main(string[] args) {
         Demo d = new Demo();
         
         int [] myArr = new int[] {59, 34, 76, 65, 12, 99};
         int sum;

         sum = d.displaySum(myArr, 5 ) ;

         /* output the returned value */
         Console.WriteLine( "Sum: {0} ", sum );
         Console.ReadKey();
      }
   }
}

C# 2 D Array or Matrix

using System;
using System.Linq;

class Demo {

   static void Main() {

      int m, n, i, j;

      // rows and columns of the matrix+
      m = 2;
      n = 2;

      int[,] arr = new int[10, 10];
      
      Console.Write("\nEnter elements of the Matrix: ");
      for (i = 0; i < m; i++) {
         for (j = 0; j < n; j++) {
            arr[i, j] = Convert.ToInt16(Console.ReadLine());
         }
      }
     
      Console.WriteLine("\nPrinting Matrix: ");
      for (i = 0; i < m; i++) {
         for (j = 0; j < n; j++) {
            Console.Write(arr[i, j] + "\t");
         }
         Console.WriteLine();
      }

      Console.ReadLine();
   }
}

C# Queue Interface

using System;
using System.Collections;

namespace Demo {
   class Program {
      static void Main(string[] args) {
         Queue q = new Queue();
         
         q.Enqueue('t');
         q.Enqueue('u');
         q.Enqueue('v');
         q.Enqueue('w'); 
         q.Enqueue('x');

         Console.WriteLine("Current queue: ");
         foreach (char c in q) Console.Write(c + " ");
                 
         Console.WriteLine();
         Console.ReadKey();
      }
   }
}

C# Multiplication of Exponents of same base

using System;
class Demo {
   static void Main() {
      double res, n, e1, e2;
        
      n = 2;
      e1 = 5;
      e2 = 4;
        
      res = e1 + e2;
        
      Console.WriteLine("Result =  {0}^{1} : {2}", n, res, Math.Pow(n, res));
      Console.ReadLine();
   }
}

Previous 1 ... 4 5 6 7 8 9 10 ... 1624 Next
Advertisements
Loading...