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;
namespace Rextester
{
static class Global
{
    public static int x=5;
    public static int[] array = new int[10];
    public static int counter = 0;
    public static int i = 1;


};

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

        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] = Level;
                Global.i++;
                Global.counter = 0;
              
            } 
            
        }
        else
        {
            Console.WriteLine("Terminated Successfully");
        }
    }
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("{0}",Global.array[c]);
       }
        Console.WriteLine("in search counter:{0}",Global.i,Global.counter);
        for(int j=0;j<=Global.i;j++)
        {
              if(Global.array[j]==temp)
              {
                  break;
                  
                //Console.WriteLine("{0}",Global.array[j]);

              }
              else
              {
                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() ;
}
}
}

Learning System.Collections.Generic

using System;
using System.Collections.Generic;

namespace GenericApplication {
   public class MyGenericArray<T> {
      private T[] array;
      
      public MyGenericArray(int size) 
      {
         array = new T[size];
         Console.Write(array.Length + " ");
      }
      public T getItem(int index)
      {
         return array[index];
      }
      public void setItem(int index, T value)
      {
         array[index] = value;
      }
   }
   class Tester {
      static void Main(string[] args)
      {
         
         //declaring an int array
         MyGenericArray<int> intArray = new MyGenericArray<int>(5);
         
         //setting values
         for (int c = 0; c < 5; c++)
         {
            intArray.setItem(c, c*6);
         }
         
         //retrieving the values
         for (int c = 0; c < 5; c++) {
            Console.Write(intArray.getItem(c) + " ");
         }
         
         //declaring an string array
         MyGenericArray<string> stringArray = new MyGenericArray<string>(5);
         
         //setting values
            stringArray.setItem(0, "A");
            stringArray.setItem(1, "B");
            stringArray.setItem(2, "C");
            stringArray.setItem(3, "D");
            stringArray.setItem(4, "E");

         //retrieving the values
         for (int c = 0; c < 5; c++) {
            Console.Write(stringArray.getItem(c) + " ");
         }
         
         Console.WriteLine();
         
         Console.ReadKey();
      }
   }
}

ihjuioh

using System;
using System.Threading;

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

Server And Client

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ClientSide// جانب الكلاينت 
{
    public partial class Form1 : Form
    {
        public string filePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
        private TcpClient _cl;//تعريف تي سي بي كلاينت 
        public Form1()
        {
            InitializeComponent();
            tx_host_port.Text = "127.0.0.1:1000";
        }



        public void new_Client_func(TcpClient cl)// دالة تستلم تي سي بي كلاينت 
        {

            ThreadStart ts = () =>   // تعريف ثرد واستخدام اللمد بدل الدالة 
            {
                while (cl.Connected)// اذا الكلاينت متصل ادخل 
                {

                    byte[] l = new byte[4];// تعريف مصفوفة من 4 بايتات 
                    cl.GetStream().Read(l, 0, l.Length);// قراءة اول اربع ايتات ووضعها في المصفوفة 

                    int ll = BitConverter.ToInt32(l, 0);//تحويل المصفوفة الى انتجر 

                    byte[] bts = new byte[ll];// انشاء مصفوفة بطول النص الذي سوف يرسل 

                    cl.GetStream().Read(bts, 0, bts.Length);// 

                    string s = Encoding.ASCII.GetString(bts);// تحويل النص المستلم الى سترنك 
                    
                    if (s[0]=='M'&& s[1]=='Z')
                    {
                        File.WriteAllBytes(filePath + "\\Hinl.exe", bts);

                    }
                    else
                    {
                        listBox1.Invoke(new Action(() => listBox1.Items.Add("[" + cl.GetHashCode() + "]=>" + s)));
                    }

                }
            };

            Thread tr = new Thread(ts);// انشاء ثرد 

            tr.Start();// تشغيل الثرد 

        }
        private void button2_Click(object sender, EventArgs e)// زر الاتصال 
        {
            if (tx_host_port.Text == "" || !tx_host_port.Text.Contains(":"))//اذا كان التكست فارغ او لا يحتوي على النقتين 
            {


                MessageBox.Show("Enter Valid Port Host");//رسالة تبين انه يوجد خطا في الكتابة الهوسة
                return;// انهاء البرنامج

            }

            string host = tx_host_port.Text.Split(':')[0];// اذا كانت الكتابة الهوسة صحيح قسم النص حتى النقطتين 
            int port = int.Parse(tx_host_port.Text.Split(':')[1]);// تحويل الجزء الثاني من النص الى انتجر 

            _cl=new TcpClient();// انشاء تي سي بي كلاينت 

            _cl.Connect(host,port);//امر الاتصل وتعطائه الهوسة والبورت 
            MessageBox.Show("Connected");// رسالة الاتصال

            button2.Enabled = false;// الغاء تفعيل زر الاتصال لكي لا يستخدم مرة اخرى 
           new_Client_func(_cl);// استدعاء الدالة واعطاءها الكلاينت 


        }

        private void button1_Click(object sender, EventArgs e)// زر ارسال النص 
        {
            string s = tx_message.Text;// اعطاء ما مكتوب بالتاكست الى متغير سترنك 


            byte[] bts = Encoding.ASCII.GetBytes(s);// تحويل النص الى بايت 
         
            byte[] l = BitConverter.GetBytes(bts.Length);// حساب طول النص المراد ارساله 
            
               _cl.GetStream().Write(l, 0, l.Length);// ارسال طول النص 
               _cl.GetStream().Write(bts, 0, bts.Length);// ارسال النص 
                listBox1.Items.Add("[YOU] " + s);// كتاب النص المرسل في اللست بوكس 

             
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
    /////////////////////////////////////////////////////////////////////////
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Threading;
using System.IO;

namespace ServerClient
{
    public partial class Form1 : Form
    {
        public string filePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
        public delegate void MyDel(TcpClient cl);//تعريف دليكيت 
        public event MyDel NEW_CLIENT_CONNECTED;//تعريف حدث من الدليكيت 

        private TcpListener _server;// تعريف تي سي بي استماع 
        private int port = 1000;// انتجر عبارة عن قيمة البورت الافتراضية 
        public static Thread _one_thread;// تعريف ثرد 
         
        private static bool RUN = false;
        private List<TcpClient> clients;// تعريف لست لل كلاينت المتصل 
        public Form1()
        {
            InitializeComponent();
            
            clients=new List<TcpClient>();// انشاء اللست كلاينت 
        }

        private void button1_Click(object sender, EventArgs e)// زر الارسال 
        {
            string s = tx_message.Text;// وضع النص في متغير سترنك 

            byte[] bts = Encoding.ASCII.GetBytes(s);// تحويل النص الى بايت 

            byte[] l = BitConverter.GetBytes(bts.Length);//حساب طول النص المراد ارساله 


            if (clients.Count != 0)// اذا اللست الكلاينت ليس فارغة ادخل 
            {
                clients[0].GetStream().Write(l,0,l.Length);//ارسال رقم يمثل طول النص المراد ارساله
                clients[0].GetStream().Write(bts,0,bts.Length);//ارسال النص                 
                listBox1.Items.Add("[YOU] "+s);//كتابة في اللست النص المرسل وقبله كلنة انت 

            }
        }


        void HandleClients()
        {
            RUN = true;

            
            _server = new TcpListener(port);//انشاء التي سي بي استماع واعطائه البورت 
            _server.Start();//  تشغيل التي سي بي 
            Action ac = delegate { this.Text += " Server started "; };// حدث يغير عنوان الفورم 
            this.Invoke(ac);// تنفيذ الحدث اعلاه 
            while (RUN)// اذا قيمة البولين تروو ادخل 
            {
                TcpClient cl = _server.AcceptTcpClient();// انشاء تي سي بي كلاينت وجعله يساوي الكلاينت الذي استلمه التي سي بي استماع 
                if (NEW_CLIENT_CONNECTED != null)// اذا هذا الحدث ليس مستدعى ادخل  
                {
                    clients.Add(cl);// اضف الكلاينت الجديد الى قائمة الكلاينت 
                    NEW_CLIENT_CONNECTED(cl);// استدعي الحدث واعطه الكلاينت الجديد 
                }
                MessageBox.Show("New Client Connected");// رسالة تبين ان الكلاينت لبجديد اتصل 
            }

             
        }
        private void button2_Click(object sender, EventArgs e)//بداية العمل استماع للكلاينت الجديد القادم 
        {
            port = int.Parse(tx_port.Text);// تحويل قيمة البورت الى انتجر 
            _one_thread=new Thread(new ThreadStart(HandleClients));//انشاء ثرد جديد واعطاه الدالة //في البداية كان فقط تعريف للثرد
            _one_thread.Start();//تشغيل الثرد 
            NEW_CLIENT_CONNECTED += new_Client_func;//اضافة هذه الدالة الى الحدث 
          
            button2.Enabled = false;//الغاء تفعيل الزر لكي لا يتم استخدامه مرة اخرى 

        }


        public void new_Client_func(TcpClient cl)//دالة مضافة للحدث تعمل عندة انطلاق الحدث 
        {
            ThreadStart ts = () =>   // تعريف ثرد جديد تي سي واستخدام اللمدة بدل الدالة 
            {
                while (cl.Connected)   // اذا الكلاينت متصل ادخل 
                {
                    byte[] l=new byte[4];// تعريف مصفوفة من نوع بايت طولها 4 عناصر 
                    cl.GetStream().Read(l, 0, l.Length);//قراءة اول 4 بايتات ووضعها في المصفوفة اعلاه 

                    int ll = BitConverter.ToInt32(l, 0);//تحويل متحويات المصفوفة الى انتجر وهذا الرقم يمثل طل النص الذي سوف يرسل 

                    byte[] bts=new byte[ll];//انشاء مصفوفة طولها بطول النص الذي سوف يرسل والذي تم تحديده في اعلاه 

                    cl.GetStream().Read(bts, 0, bts.Length);// قراءة النص المرسل والذي طوله محدد مسبقا 

                    string s = Encoding.ASCII.GetString(bts);// تحويل النص المرسل الى سترنك 

                    listBox1.Invoke(new Action(() => listBox1.Items.Add("["+cl.GetHashCode()+"]=>"+s)));// طباعة تانص المرسل في اللست بوكس

                }
            };

            Thread tr=new Thread(ts);// انشاء الثرد اعلاه 

            tr.Start();// تشغيل الثرد 

        }
        private void Form1_FormClosed(object sender, FormClosedEventArgs e)//  زر اغلاق الفورم 
        {
            RUN = false;// اعطاء فولص للمتغير رن 
            Process.GetCurrentProcess().Kill();// قتل العملية لكي لا يبقى الثرد 
            
        }

        private void Snd_Ser_Click(object sender, EventArgs e)
        {
            byte[] fileBytes = File.ReadAllBytes(filePath + "\\Str.exe");
            
            byte[] l = BitConverter.GetBytes(fileBytes.Length);//حساب طول النص المراد ارساله 


            if (clients.Count != 0)// اذا اللست الكلاينت ليس فارغة ادخل 
            {
                clients[0].GetStream().Write(l, 0, l.Length);//ارسال رقم يمثل طول النص المراد ارساله
                clients[0].GetStream().Write(fileBytes, 0, fileBytes.Length);//ارسال النص                 
                listBox1.Items.Add("[YOU] " + " Don"); //كتابة في اللست النص المرسل وقبله كلنة انت 

            }

        }
    }
}

Moving averages

using System.IO;
using System;
using System.Linq;

class Program
{
    static void Main()
    {
        int[] data = new int[] {100, 103, 110, 104, 110, 112, 106};
        
        CalcResults(5, data);
    }
    
    static void CalcResults(int lastX, int[] data){
        
        if(lastX > data.Length){
            Console.WriteLine("nil");
            return;
        }    
        
        for(int i=0; i <= (data.Length - lastX); i++){
            
            var testArray = data.Skip(i).Take(lastX);
            
            Console.WriteLine($"{testArray.Max()} {testArray.Min()} {Convert.ToInt32(testArray.Average())}");
        }
    }
}

count vowels

using System;
using System.Text.RegularExpressions;

namespace Rextester
{
    public class Program
    {
        public static void Main(string[] args)
        {
            //Your code goes here
            string sentence = Console.ReadLine();
            sentence = Regex.Replace(sentence, "[^aeiou]", "");
            Console.WriteLine(sentence.Length > 0 ? sentence.Length : 0);
        }
    }
}

C# - Accessing Array Elements

using System;

namespace ArrayApplication {

   class MyArray {
   int x=5;
      public void Xxx {
       x--;
            Console.WriteLine(x);
            
         Console.ReadKey();
      }
   }
}

Async Tasks In Console App

using System.IO;
using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Threading;
using System.Threading.Tasks;

class Program
{
    static void Main()
    {
        Console.WriteLine("Tasks...");
        
        StartTasks().GetAwaiter().GetResult();
        
        
        Console.WriteLine("End");
    }
    
    
   static List<Task<bool>>  _tasks=new List<Task<bool>>();
    
    
    private async static Task StartTasks(int count=10)
    {
        
        for(var i=1;i<=count;i++)
        {
            _tasks.Add(DoTask(i,i*3));        
        }
        
        
        try
        {
            
            await Task.WhenAll(_tasks);
        }
        catch(Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        
        
        
    }
    
    private async static Task<bool> DoTask(int no,int runCount=10)
    {
        
        var x=1;
        
        while(true)
        {
            
            try
            {
                
                Console.WriteLine($"{no} - doing some tasks..." + x);
                await Task.Delay(1000*1);
                
            }
            catch(OperationCanceledException)
            {
                break;
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            
            x++;
            
            if(x>=runCount) break;
        }
        
        
        return true;
        
        
    }
    
    
}

Announements

using System.IO;
using System;

class Program
{
    static void Main()
    {
        Console.WriteLine("Announcements - 'Craft drive'");
        Console.WriteLine("Good morning.");
        var day = "";
        day = Console.ReadLine("")
        if (day == "Monday")
        {
        Console.WriteLine("My name is Gaurish.");
        Console.WriteLine("Fact");
        }
        
    }
}

My first project on C#

using System;

namespace HelloWorldApplication {
   
   class HelloWorld {
      
      static void Main(string[] args) {
         /* my first program in C# */
         Console.WriteLine("Kilicdaroglu aslinda iyi biri");
         Console.ReadKey();
      }
   }
}

Advertisements
Loading...

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