2 Aralık 2010 Perşembe

2 Boyutlu Dizi Örneği

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

namespace Console_dizi_cokboyutlu
{
class Program
{
static void Main(string[] args)
{
int[,] dizi = new int[4, 3];
int i, j,sutun=1,satir=1;
for (i = 0; i < 4; i++)
{
for (j = 0; j < 3; j++)
{
Console.SetCursorPosition(sutun, satir);
Console.Write(".");
sutun += 6;
}
sutun = 1;
satir += 4;
}
sutun = 1; satir = 1;
for (i = 0; i < 4; i++)
{
for (j = 0; j < 3; j++)
{
Console.SetCursorPosition(sutun, satir);
dizi[i, j] = int.Parse(Console.ReadLine());
sutun += 6;
}
sutun = 1;
satir += 4;
}
Console.ReadKey();
}
}
}

30 Eylül 2010 Perşembe

C# da Değişken Tipleri

Tam Sayı Değişken Tipleri
-------------------------
Type Size Range (aralık)
sbyte 8 -128 to 127
byte 87 0 to 255
short 16 -32768 to 32767
ushort 16 0 to 65535
int 32 -2147483648 to 2147483647
uint 32 0 to 4294967295
long 64 -9223372036854775808 to 9223372036854775807
ulong 64 0 to 18446744073709551615
char 16 0 to 65535

Ondalıklı Değişken Tipleri
--------------------------
Type Size Range
float 32 7 digits 1.5 x 10-45 to 3.4 x 1038
Double 64 15-16 digits 5.0 x 10-324 to 1.7 x 10308
Decimal 128 28-29 decimal places 1.0 x 10-28 to 7.9 x 1028

Diğer Değişken Tipleri
----------------------
string karaktersel ifadeler için kullanılır
bool mantıksal değişken. (true ve false değerlerini alır)

C# da Parse Methodu ile Tip Dönüşümleri

Açıklama:
---------
Parse, String tipteki verilerin sayısal tiplere dönüşümünü sağlayan methoddur.

Kullanımı:
----------
tip_adı.Parse(string veri);

Örnek Kodlama:
--------------
using System;
using System.Collections.Generic;
using System.Text;

namespace console_008
{
class Program
{
static void Main(string[] args)
{
int a, b, t;
Console.Write("1.Sayıyı Giriniz :");
a = int.Parse(Console.ReadLine());
Console.Write("2.Sayıyı Giriniz :");
b = int.Parse(Console.ReadLine());
t = a + b;
Console.WriteLine("Sayıların Toplamı :"+t);
Console.ReadKey();
}
}
}

C# Console da ReadLine( ) komutu

Açıklama:

Console ortamında klavyeden bilgi okutmak amacıyla kullanılan bir komuttur. Okutulan bilgiler her zaman string tipindedir. Dolayısıyla sayı ya da başka tipte kullanım yapılmak istendiğinde tip dönüşümü yapılması gerekir. Tip dönüşümleri başka derslerde açıklanacaktır.

Örnek Kodlama :
---------------

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

namespace console_007
{
class Program
{
static void Main(string[] args)
{
string s;
s = Console.ReadLine();
Console.WriteLine("Girdiğiniz Metin : "+s);
Console.ReadKey();
}
}
}

14 Mayıs 2010 Cuma

C# da Text Dosyadan Kayıt Silme İşlemi

string ad, soyad, tel, yol, yol2;
FileStream dosya, dosya2;

private void Form1_Load(object sender, EventArgs e)
{
yol = Application.StartupPath + "\\telefon.txt";
yol2 = Application.StartupPath + "\\gecici.txt";
}

private void button4_Click(object sender, EventArgs e)
{
dosya = new FileStream(yol, FileMode.OpenOrCreate, FileAccess.Read);
StreamReader okuma;
okuma = new StreamReader(dosya);

dosya2 = new FileStream(yol2, FileMode.Create, FileAccess.Write);
StreamWriter yazma;
yazma = new StreamWriter(dosya2);

do
{
ad = okuma.ReadLine();
soyad = okuma.ReadLine();
tel = okuma.ReadLine();
if (textBox1.Text != ad)
{
yazma.WriteLine(ad);
yazma.WriteLine(soyad);
yazma.WriteLine(tel);
}
}
while (ad != null);
okuma.Close();
yazma.Close();
File.Delete(yol);
File.Move(yol2, yol);
}

C# da Text Dosyada Kayıt Arama İşlemi

string ad, soyad, tel, yol, yol2;
FileStream dosya, dosya2;

private void Form1_Load(object sender, EventArgs e)
{
yol = Application.StartupPath + "\\telefon.txt";
yol2 = Application.StartupPath + "\\gecici.txt";
}

private void button3_Click(object sender, EventArgs e)
{
Boolean bul;
bul = false;
dosya = new FileStream(yol, FileMode.OpenOrCreate, FileAccess.Read);
StreamReader okuma;
okuma = new StreamReader(dosya);
do
{
ad = okuma.ReadLine();
soyad = okuma.ReadLine();
tel = okuma.ReadLine();
if (textBox1.Text == ad)
{
bul = true;
textBox2.Text = soyad;
textBox3.Text = tel;
}
}
while (ad != null);
if (bul == false) MessageBox.Show("Aradığınız Kayıt Bulunamadı");
okuma.Close();
}

C# da Text Dosyadan Okuma İşlemi (Listeleme)

string ad, soyad, tel, yol, yol2;
FileStream dosya, dosya2;

private void Form1_Load(object sender, EventArgs e)
{
yol = Application.StartupPath + "\\telefon.txt";
yol2 = Application.StartupPath + "\\gecici.txt";
}

private void button2_Click(object sender, EventArgs e)
{
dosya = new FileStream(yol, FileMode.OpenOrCreate, FileAccess.Read);
StreamReader okuma;
okuma = new StreamReader(dosya);
listBox1.Items.Clear();
listBox2.Items.Clear();
listBox3.Items.Clear();
do
{
ad = okuma.ReadLine();
soyad = okuma.ReadLine();
tel = okuma.ReadLine();

if (ad != null)
{
listBox1.Items.Add(ad);
listBox2.Items.Add(soyad);
listBox3.Items.Add(tel);
}
}
while (ad != null); //Şart sağlandığı sürece döngü çalışır.
okuma.Close();
}

C# da Text Dosyaya Kayıt İşlemi

string ad, soyad, tel, yol, yol2;
FileStream dosya, dosya2;

private void Form1_Load(object sender, EventArgs e)
{
yol = Application.StartupPath + "\\telefon.txt";
yol2 = Application.StartupPath + "\\gecici.txt";
}
//Kaydet Butonu
private void button1_Click(object sender, EventArgs e)
{
ad = textBox1.Text;
soyad = textBox2.Text;
tel = textBox3.Text;
dosya = new FileStream(yol, FileMode.Append, FileAccess.Write);
StreamWriter yazma;
yazma = new StreamWriter(dosya);
yazma.WriteLine(ad);
yazma.WriteLine(soyad);
yazma.WriteLine(tel);
yazma.Close();
}

25 Mart 2010 Perşembe

C# da Operatörler

Matematiksel Operatörler
........................
+ Toplama
- Çıkarma
* Çarpma
/ Bölme
++ Artırma
-- Eksiltme
% Mod

Mantıksal Operatörler
.....................
< Küçüktür
> Büyüktür
<= Küçük Eşit
>= Büyük Eşit
== Eşit
!= Eşit Değil
&& Ve
|| Veya

Diğer Operatörler
.................
= Aktarma
// Açıklama satırı

24 Mart 2010 Çarşamba

C# da Foreach Döngüsü

foreach ile nesneler döndürülebilir.
Örneğin aşağıdaki program, formun üzerindeki tüm textBox lara "Kütahya" yazar. Ayrıca formdaki textBox ları ve combobox ları sayarak sonuçları labellarda gösterir.


private void Form1_Load(object sender, EventArgs e)
{
int i=0, j=0;
foreach (Control cont in this.Controls)
{
if (cont is TextBox)
{
cont.Text = "Kütahya";
i++;
}
if (cont is ComboBox) j++;
}
label1.Text = i.ToString();
label2.Text = j.ToString();
}

C# ile mail gönderme


Öncelikle projeye kod sayfasının en üstünde aşağıdaki şekilde .net mail sınıfını dahil etmeniz gerekir.

using System.Net;
using System.Net.Mail;
.
.
Gönderinin adresi hotmail ise smtp adresi: smtp.live.com
gmail için ise : smtp.gmail.com
Her ikisi için port numarası 587 dir.
.
.
Form Kodu:

private void button1_Click(object sender, EventArgs e) //Gönder butonu
{
SmtpClient istemci = new SmtpClient(this.gonderenSMTP.Text,Convert.ToInt32(port.Text));
MailAddress gon = new MailAddress(this.gonderenMail.Text, this.gonderenAd.Text);
MailAddress alan = new MailAddress(this.kime.Text, this.kime.Text);
MailMessage msg = new MailMessage(gon, alan);
msg.Subject = this.baslik.Text;
msg.Body = this.mail.Text;
istemci.EnableSsl = true;
if (label8.Text != "")
{
Attachment ekle = new Attachment(@label8.Text);
msg.Attachments.Add(ekle);
}
System.Net.NetworkCredential SMTPUserInfo = new System.Net.NetworkCredential(this.gonderenMail.Text, this.gonderenSifre.Text);
istemci.UseDefaultCredentials = false;
istemci.Credentials = SMTPUserInfo;
istemci.Send(msg);
}

private void button2_Click(object sender, EventArgs e) //Dosya ekle butonu
{
openFileDialog1.ShowDialog();
label8.Text = openFileDialog1.FileName.ToString();
}

C# da Switch( ) Yapısı

Açıklama:
if benzeri bir komut yapısıdır. Belli bir değişkenin aldığı değerlere göre işlem yapılmasını sağlar.

Kullanımı:
.
.
.
switch(değişken)
{
case değer1: komut1; break;
case değer2: komut2; break;
case değer3: komut3; break;
case default: komut4; break;
}

Eğer değişken değerlerin hiç birine sahip değilse default dan sonraki komutlar çalışır. Default yazılmak zorunda değiildir.


private void button1_Click(object sender, EventArgs e)
{
int secim;
secim = int.Parse(textBox1.Text);
switch (secim)
{
case 1: label1.Text = "0"; break;
case 2: label1.Text = "1"; break;
case 3: label1.Text = "2"; break;
default: label1.Text = "Geçersiz Seçim"; break;
}
}

C# da Do While ve While Döngüleri

While Döngüsü:
---------------
Belli bir şarta bağlı olarak istenilen komutların çalıştırılmasını sağlar.
Kullanımı:
---------
While (şart)
{
...komutar
}

Komutları hiç çalıştırmadan döngü başında şarta bakar.
Şart sağlandında komutları çalıştırır ve tekrar döngü başına gelir.
Şart sağlandığı sürece komutları çalıştırmaya devam eder.

Do ... While Döngüsü:
---------------------
Belli bir şarta bağlı olarak istenilen komutların çalıştırılmasını sağlar.
Kullanımı:
---------
Do
{
...komutlar
}
while (şart);

Şarta bakmaksızın komutları bir defa çalıştırır ve daha sonra şarta bakar.
Şart sağlandığı sürece komutları çalıştırmaya devam eder.

Örnek Kod:

private void Form1_Load(object sender, EventArgs e)
{
int x;
x=1;

while (x < 10)
{
listBox1.Items.Add(x);
x++;
}

do
{
listBox2.Items.Add(x);
x++;
}
while (x < 10);

}

C# da if komutu

Amacı: Belli bir şarta ya da şartlara bağlı olarak istenilen komutların çalışmasını sağlar.
Kullanım1: if (şart) komut;
Kullanım2: if (şart) komut1;
else komut2;
Kullanım3: if (şart1) komut1;
else if (şart2) komut2;
else komut3;

Örnek Kod:
----------
private void button1_Click(object sender, EventArgs e)
{
sayi = int.Parse(textBox1.Text);
if (sayi < 0) label2.Text = "Negatif";
else if (sayi > 0) label2.Text = "Pozitif";
else label2.Text = "Sıfır";
}

C# da for döngüsü

Amacı:
İstenilen komutların belli sayıda çalıştırılmasını sağlar.
Kullanımı:
for (dongu değişkeni=başlangıç değeri;şart;artış miktarı) komut;
Eğer komutlar birden fazla ise parantez içerisine alınır.

Örnek Kod:
inputbox ile girilen 5 adet sayının en büyük ve en küçüğünü bulan program.

private void Form1_Load(object sender, EventArgs e)
{
for (i = 1; i <= 5; i++)
{
sayi = int.Parse(Microsoft.VisualBasic.Interaction.InputBox(i.ToString()+".Sayı Giriniz", "Sayı Girişi", "", 100, 120));
if (i == 1)
{
enb = sayi;
enk = sayi;
}
if (sayi > enb) enb = sayi;
if (sayi < enk) enk = sayi;
listBox1.Items.Add(sayi.ToString());
}
textBox1.Text = enb.ToString();
textBox2.Text = enk.ToString();
}