Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

Compile and Execute C# Sharp Online

using System.IO;
using System;

class Program
{
    static void Main()
    {
        Console.WriteLine("Enter 3 fruits");
        Console.WriteLine();
        
        string fruit = String.Empty;
        string[] fruitList = new string[3];
        
        for (int x = 0; x < 3; x++) 
        {
            fruitList[x] = Console.ReadLine();
        }
        
        for (int i = 0; i < fruitList.Length; i++) 
        {
            for (int j = i + 1; j < fruitList.Length; j++) 
            {
                if (fruitList[i].Length < fruitList[j].Length) 
                {
                    fruit = fruitList[i];
                    fruitList[i] = fruitList[j];
                    fruitList[j] = fruit;
                }
            }
            Console.WriteLine(fruitList[i]);

        }

        
    }
}

Matrix

//Rextester.Program.Main is the entry point for your code. Don't change it.
//Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace Rextester
{
    public class Program
    {
        public static void Fill(int [,]matr, int n, int m)
        {
            Random rand = new Random();
            for(int i=0;i<n;i++)
            {
                for(int j=0;j<m;j++)
                {
                    matr[i,j] = rand.Next(0, 133);
                }
            }
        }
        static void Swap<T>(ref T a, ref T b)
        {
            T c = a;
            a = b;
            b = c;
        }
        public static void Print(int [,]matr, int n, int m)
        {
            for(int i=0;i<n;i++)
            {
                for(int j=0;j<m;j++)
                {
                    Console.Write(matr[i,j] + "\t");
                }
                Console.WriteLine();
            }
        }
        public static void Sort(int [,]matr, int n, int m)
        {
            for(int j=0;j<m;j++)
            {
                for(int i=0;i<n;i++)
                {
                    for(int z=0;z<n;z++)
                    {
                        if(matr[i,j] < matr[z,j])
                        {
                            Swap(ref matr[i,j], ref matr[z,j]);
                        }
                    }
                }
            }
        }
        public static void Main(string[] args)
        {
            Console.WriteLine("n = ");
            int n = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("m = ");
            int m = Convert.ToInt32(Console.ReadLine());
            int [,]matr = new int[n,m];
            Fill(matr, n, m);
            Console.WriteLine("Matrix");
            Print(matr, n, m);
            Sort(matr, n, m);
            Console.WriteLine("After sorting");
            Print(matr, n, m);
        }
    }
}

C# - Handling Exceptions

using System;

namespace ErrorHandlingApplication {

   class DivNumbers {
      int result;
      
      DivNumbers() {
         result = 0;
      }
      
      public void division(int num1, int num2) {
          /*
           result = num1 / num2;
           Console.WriteLine("Result: {0}", result);
           */
           
         try {
            result = num1 / num2;
            // int r1 = num1 + num2;
         } catch (DivideByZeroException exp) {
            Console.WriteLine("Exception caught: :-\n{0}", exp);
             Console.WriteLine("end! ");
         } finally {
            Console.WriteLine("Result: {0}", result);
         }
      }
      
      static void Main(string[] args) {
         DivNumbers d = new DivNumbers();
         d.division(25, 9);
         Console.ReadKey();
      }
   }
}

abstract override

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

namespace VirtualFunctionApplication {
   
 
   
    abstract class Base  {
     
        public  Base() {
            Console.WriteLine("This is base class");
        }
        public abstract void func();
      }
    
    class Derivative : Base {
        public  Derivative() {
            Console.WriteLine("This is Derivative class");
        }
        public override void func(){
            Console.WriteLine("This is base override function");
            
        }
        
    }
      
   
   
   class MainClass {
     
      static void Main(string[] args) {
        Base b = new Derivative();
        b.func();
      }
   }
}

sdfgjk

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(2000);
         
         //now abort the child
         Console.WriteLine("In Main: Aborting the Child thread");
         
         childThread.Abort();
         Console.ReadKey();
      }
   }
}

C# Properties

using System;
namespace tutorialspoint {
   class Student {
      private string code = "N.A";
      private string name = "not known";
      private int age = 0;
      
      // Declare a Code property of type string:
      public string Code {
         get {
            return code;
         }
         set {
            code = value;
         }
      }
      
      // Declare a Name property of type string:
      public string Name {
         get {
            return name;
         }
         set {
            name = value;
         }
      }
      
      // Declare a Age property of type int:
      public int Age {
         get {
            return age;
         }
         set {
            age = value;
         }
      }
      public override string ToString() {
         return "Code = " + Code +", Name = " + Name + ", Age = " + Age;
      }
   }
   
   class ExampleDemo {
      public static void Main() {
      
         // Create a new Student object:
         Student s = new Student();
         
         // Setting code, name and the age of the student
         s.Code = "001";
         s.Name = "Zara";
         s.Age = 9;
         Console.WriteLine("Student Info: {0}", s);
         
         //let us increase age
         s.Age += 1;
         Console.WriteLine("Student Info: {0}", s);
         Console.ReadKey();
      }
   }
}

Calling Methods in C#

using System;
namespace Rextester
{
static class Global
{
    public static int x=4;
    public static int[] array = new int[10];
    public static int counter = 0;
    public static int i = 0;
     public static int Level = 0;


};

class Test
{
    //...............................
     public void LevelLoad()
    {
         Global.Level = 0;
        Random rnd = new Random();
        Global.Level = rnd.Next(3, 8);
        Console.WriteLine("Executed Successfully First Level={0} Counter={1}",Global.Level,Global.counter);
       Search(Global.Level);
        

        if (Global.x != 0)
        {
            
           Console.WriteLine("Executed Successfully in the x if x:{0}",Global.x);
            if (Global.counter != 0)
            {
                 Console.WriteLine("---------------in the counter if");
                Global.x--;
                Global.array[Global.i] = Global.Level;
                Global.i++;
               
              
            } 
        }
        else
        {
            Console.WriteLine("Terminated Successfully");
        }
          Global.counter=0;
    }
public void Search(int temp)
    {
      // int j = 0;
       Console.WriteLine("Search function called i:{0}",Global.i);
       for(int c=0;c<Global.i;c++)
       {
           Console.WriteLine(Global.array[c]);
       }
        Console.WriteLine("in search counter:{0},:{0}",Global.i,Global.counter);
        for(int j=0;j<=Global.i;j++)
        {
              if(Global.array[j]!=temp)
              {
                  
                Global.counter++;
                Console.WriteLine("In the **********");    
              }
            
          }
    }
//...................................................
public void SayHello()
{
Console.WriteLine("Jithu.......bb") ;
}
}
class Program
{
static void Main(string[] args)
{
Test[] t = new Test[5];
int z;
for(z=0;z<5;z++)
{
     Console.WriteLine(" {0} th Iteration",z);
      Console.WriteLine("..................................");
    t[z]=new Test();
    t[z].LevelLoad();
    Console.WriteLine("..................................");
}
Console.ReadKey() ;
}
}
}

C# - Interfaces

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

namespace InterfaceApplication {
   
  public interface ITransactions {
      // interface members
      void showTransaction();
      double getAmount();
  }
   
   public class Transaction : ITransactions{
      private string tCode;
      private string date;
      private double amount;
      
      public Transaction() {
         tCode = " ";
         date = " ";
         amount = 0.0;
      }
      
      public Transaction(string c, string d, double a) {
         tCode = c;
         date = d;
         amount = a;
      }
      
    //   public double getAmount() {
    //      return amount;
    //   }
      
      public void showTransaction() {
         Console.WriteLine("Transaction: {0}", tCode);
         Console.WriteLine("Date: {0}", date);
         Console.WriteLine("Amount: {0}", getAmount());
      }
   }
   
   class Tester {
     
      static void Main(string[] args) {
         Transaction t1 = new Transaction("001", "8/10/2012", 78900.00);
         Transaction t2 = new Transaction("002", "9/10/2012", 451900.00);
        //  t1.showTransaction();
        //  t2.showTransaction();
         Console.ReadKey();
      }
   }
}

Test

using System; 

namespace charCountDemo {  
   public class Example { 
      public static void Main(){ 
         String str = "abracadabra"; 
         int []charCount = new int[256]; 
         int length = str.Length; 
         for (int i = 0; i < length; i++){
            charCount[str[i]]++; 
         }
         int maxCount = -1; 
         char character = ' ';
         for (int i = 0; i < length; i++){ 
            if (maxCount < charCount[str[i]]){ 
               maxCount = charCount[str[i]]; 
               character = str[i]; 
            } 
         }  
         Console.WriteLine("The string is: " + str);
         Console.WriteLine("The highest occurring character in the above string is: " + character);
         Console.WriteLine("Number of times this character occurs: " + maxCount); 
      } 
   } 
}

C# Basic Syntax

using System;
using RectangleApplication;

namespace MyNamespace {
    
   class MyTest {
       static void Main(string[] args){
           Console.WriteLine("It works!!");
           
           Rectangle r = new Rectangle();
           r.Acceptdetails();
           r.Display();
       }
   }
    
}

namespace RectangleApplication {
   
   class Rectangle {
      // member variables
      double length;
      double width;
      
      public void Acceptdetails() {
         length = 4.5;    
         width = 3.5;
      }
      
      public double GetArea() {
         return length * width; 
      }
      
      public void Display() {
         Console.WriteLine("Length: {0}", length);
         Console.WriteLine("Width: {0}", width);
         Console.WriteLine("Area: {0}", GetArea());
      }
   }
   
   class ExecuteRectangle {
   
    //   static void Main(string[] args) {
    //      Rectangle r = new Rectangle();
    //      r.Acceptdetails();
    //      r.Display();
    //      Console.ReadLine(); 
    //   }
   }
}











Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.