11 Eylül 2015 Cuma

c# HttpWebRequest oluşturma

 HttpWebRequest request = (HttpWebRequest)WebRequest.Create (args[0]);

            // Set some reasonable limits on resources used by this request
            request.MaximumAutomaticRedirections = 4;
            request.MaximumResponseHeadersLength = 4;
            // Set credentials to use for this request.
            request.Credentials = CredentialCache.DefaultCredentials;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse ();

            Console.WriteLine ("Content length is {0}", response.ContentLength);
            Console.WriteLine ("Content type is {0}", response.ContentType);

            // Get the stream associated with the response.
            Stream receiveStream = response.GetResponseStream ();

            // Pipes the stream to a higher level stream reader with the required encoding format. 
            StreamReader readStream = new StreamReader (receiveStream, Encoding.UTF8);

            Console.WriteLine ("Response stream received.");
            Console.WriteLine (readStream.ReadToEnd ());
            response.Close ();
            readStream.Close ();

9 Eylül 2015 Çarşamba

c# matrix title yapımı

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

namespace MatrixConsole
{
    class Program
    {
        public static Random random = new Random();
        public static String appname = "Matrix Console";
        public static bool animated = true;

        static void Main(string[] args)
        {
            Thread mThread = new Thread(MatrixConsole);
            mThread.Start();
            while(mThread.IsAlive)
            {
                Thread.Sleep(500);
            }
        }


       private static void MatrixConsole()
       {
            while (animated)
            {
                int i = 0, j = 0, k = 0, r = 0, s = 0, t = 0, u = 300, umin = 8, umax = 10,
                    smin = 20, smax = 40, lmin = 1, lmax = 100, rmin = 5, rmax = 8, hmin = 1,
                    hmax = 8, mmin = 500, mmax = 900;
                String cl = "", rl = "", mt = "", ct = "", h = "";
                char[] titleArr = appname.ToCharArray();
                while (i < appname.Length)
                {
                    cl = titleArr[i].ToString();
                    r = random.Next(rmin, rmax);
                    j = 0;
                    while (j <= r)
                    {
                        s = random.Next(lmin, lmax);
                        rl = GenLetter(s);
                        s = random.Next(hmin, hmax);
                        if (s > (hmax / 2))
                        {
                            h = "";
                            t = 0;
                            while (t <= s)
                            {
                                h += " ";
                                t++;
                            }
                        }
                        else { h = ""; }
                        mt = ct + h + rl;
                        if (h != "")
                        {
                            k = 0;
                            while (k < h.Length)
                            {
                                h = h.Substring(k, h.Length - k - 1);
                                mt = ct + h + rl;
                                Console.Title = mt;
                                k++;
                                s = random.Next(smin, smax);
                                Thread.Sleep(s);
                                s = random.Next(lmin, lmax);
                                rl = GenLetter(s);
                            }
                        }
                        else { Console.Title = mt; }
                        j++;
                        s = random.Next(1, 3);
                        if (s > 1) { u -= r; }
                        if (u < umin) { u = umax; }
                        s = random.Next(umin, u);
                        Thread.Sleep(s);
                    }
                    ct += cl;
                    Console.Title = ct;
                    i++;
                }
                r = random.Next(mmin, mmax);
                Thread.Sleep(r);
            }
        }

        private static String GenLetter(int r)
        {
            return ((char)('a' + (r - 1))).ToString();
        }
    }
}

4 Eylül 2015 Cuma

C# LİST KULLANIMI

C# LİST KULLANIMI

List<string> gunler = new List<string>();
 //listeye eleman ekleme
 gunler.Add("pazartesi");
 gunler.Add("salı");
 gunler.Add("cumartesi");
 //listeyi ters çevirme
 gunler.Reverse();
 //listenin eleman sayısını öğrenme
 Console.WriteLine("eleman sayısı:{0}", gunler.Count);
 //listeden eleman silme
 gunler.Remove("salı");
 foreach (string eleman in gunler)
 {
     Console.WriteLine(eleman);
 }

C# ile dizinin boş elemanlarını temizleme

C# ile dizinin boş elemanlarını temizleme


Bir string dizimiz olsun ve içinin dolu olduğunu varsayalım:
1
string[] isimler = new string[10];
Dizi elemanlarıı uc uca ekleceyeceğimiz değişken:
1
string Isim = String.Empty;
Bir döngü ile dizinin tüm elemanlarını birleştirelim:
1
2
3
4
5
6
7
for (int i = 0; i < isimler.Length; i++)
{
    if (!String.IsNullOrEmpty(isimler[i]))
    {
        Isim += isimler[i] + "|";
    }
}
String.IsNullOrEmpty methodu string değerin boş yada değersiz olduğunu döndürür.
Daha sonra Isim değişkenine dizi elemanını ve “|” karakterini ekledik.
Şimdi birleştirilen diziyi ayrıştıralım:
1
string[] YeniIsimler = Isim.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
Split methodu metni parçalamaya yarar. StringSplitOptions.RemoveEmptyEntries ile de boş elemanları döndürmedik.
İsterseniz işlemi method haline de getirebiliriz:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
private string[] CleanNulled(string[] array)
{
    string Items = String.Empty;
 
    for (int i = 0; i < array.Length; i++)
    {
        if (!String.IsNullOrEmpty(array[i].Trim()))
        {
            Items += array[i] + "|";
        }
    }
 
    string[] clean = Items.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
    return clean;
}
Kullanımı ise şu şekilde:
1
2
string[] AlinanIsimler = new string[10];
string[] Isimler = CleanNulled(AlinanIsimler);