You are on page 1of 8

1

Bölüm

C#'A GİRİŞ 1
1. Editörün tanıtılması

2. Konsol uygulaması

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

namespace ornek1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Merhaba");
Console.ReadLine();
}
}
}

Namespace: .NET framework olarak isimlendirilen sınıf kitaplığındaki sınıfların saklandığı yapılardır.
Class: sınıf
WriteLine: Ekrana yazdırıp alt satıra geç (Write)
ReadLine: Ekrandan okuma (Read, ReadKey)

3. Form uygulaması

ornek2
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ornek2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)


{
MessageBox.Show("Merhaba " + textBox1.Text);
}
}
}

Prof. Dr. Fahri VATANSEVER


2

Visual Studio ile hazırlanan C# projeleri için otomatik olarak “Program.cs” kodu üretilmekte ve
programlar buradaki “Main()” metodundan itibaren çalışmaya başlamaktadırlar. Burada Application
sınıfının Run metodunda projenin başlangıç formunun ismi verilmektedir.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace ornek2
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}

4. Tanımlayıcılar

İsimlendirme kuralları: Sadece harf, rakam veya altçizgi // harf veya altçizgi ile başlamalı // 77 tane
anahtar sözcük kullanılamaz

C#’ta büyük-küçük harf duyarlılığı vardır.


Açıklama satırları // veya /* …. */
Her komut satırı ; ile biter. Yalnız blok başlatan ifadelerden sonra kullanılmaz.
Satırlar bölünebilir.

5. Değişkenler

.NET uyumlu her programlama dili Common Type System (ortak tip sistemi) kurallarına bağlı kalmak
zorundadır.

Değişken bildirimi:
Tip değişken adı;

System.Byte.MinValue
System.Byte.MaxValue

Değerlerin sonuna float için f veya F, double için d veya D konulabilir.

Prof. Dr. Fahri VATANSEVER


3

C# Byte .NET Kapasite


byte 1 System.Byte 0 ile 255
sbyte 1 System.Sbyte -128 ile 127
short 2 System.Int16 -32768 ile 32767
ushort 2 System.Uint16 0 ile 65535
int 4 System.Int32 -2 147 483 647 ile 2 147 483 647
uint 4 System.Uint32 0 ile 4 294 967 295
long 8 System.Int64 ile 9 223 372 036 854 775 808
ulong 8 System.Uint64 0 ile 18 446 744 073 709 551 615
float 4 System.Single 1,5.10-45 ile 3,4.1038
double 8 System.Double 5.10-324 ile 1,7.10308
char 2 System.Char UNICODE karakter
string System.String Karakter dizisi
bool 1 System.Boolean Mantıksal değer (True veya False)
decimal 16 System.Decimal Finansal veri (max. 28 hane)
datetime
arraylist

Ornek4
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using gercel = System.Single;

namespace ornek4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public gercel x = 1000f;
private void button1_Click(object sender, EventArgs e)
{
byte a;
System.Int32 b=100;
gercel c;
string d;
char z = 'A';
const float pi = 22/7;
var y="Elektronik";
string yol = "C:\\A\\b.dat";
string yol1 = @"C:\A\b.dat";
textBox1.Text = y.ToString(); //y.GetType().ToString();
}

private void button2_Click(object sender, EventArgs e)


{
textBox1.Text=x.ToString();
}
}
}

Tipler arası dönüşümler

değişken1 = (tip) değişken2 ;

Prof. Dr. Fahri VATANSEVER


4

Stringe dönüştürmek: ToString()

değişken.ToString()

Convert.
ToByte(…)
ToSbyte
ToInt16
ToInt32
ToInt64
ToUInt16
ToUInt32
ToUInt64
ToSingle
ToDouble
ToChar
ToString
ToBoolean
ToDecimal
ToDateTime
ToBase64String
ToBase64CharArray

Ornek5
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ornek5
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)


{
float a, b, c;
a = Convert.ToSingle(textBox1.Text);
b = Convert.ToSingle(textBox2.Text);
c = a + b;
textBox3.Text = Convert.ToString(c);
}
}
}

Prof. Dr. Fahri VATANSEVER


5

Parse
System.Single.Parse(…)

Ornek6
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ornek6
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)


{
int a;
double b;
a = System.Int32.Parse(textBox1.Text);
b = Math.Sqrt(a);
MessageBox.Show(Convert.ToString(b));
}
}
}

Dizi tanımlamak:

tip[] dizi adı = new tip[ eleman sayısı ];

Prof. Dr. Fahri VATANSEVER


6

6. Operatörler

Matematiksel
Operatör Açıklama
+ Toplama
- Çıkarma
* Çarpma
/ Bölme
% Kalan

++ Arttırma
-- Azaltma

+= Topla-aktar
-= Çıkar-aktar
*= Çarp-aktar
/= Böl-aktar

Karşılaştırma
Operatör Açıklama
== Eşit
!= Eşit değil
> Büyük
< Küçük
>= Büyük eşit
<= Küçük eşit

Mantıksal
Operatör Açıklama
&& VE
|| VEYA
! DEĞİL

Bit düzeyinde işlem


Operatör Açıklama
& VE
| VEYA
^ ÖZEL VEYA
~ DEĞİL

Kaydırma
Operatör Açıklama
>> Sağa
<< Sola

Prof. Dr. Fahri VATANSEVER


7

7. Karşılaştırma deyimleri

if-else if-else

Ornek7
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ornek7
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)


{
int s;
s = Convert.ToInt32(textBox1.Text);
if (s > 0) label2.Text = "Pozitif";
else if (s == 0) label2.Text = "Sıfır";
else label2.Text = "Negatif";
}
}
}

switch-case

Ornek8
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ornek8
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_DoubleClick(object sender, EventArgs e)


{
int not;
not = Convert.ToInt32(textBox1.Text);
switch (not)
{
case 0: label2.Text = "Zayıf"; break;
case 1: label2.Text = "Orta"; break;
case 2: label2.Text = "İyi"; break;
case 3: label2.Text = "Çok iyi"; break;
}
}
}
}

Prof. Dr. Fahri VATANSEVER


8

8. Döngü komutları

for

Ornek9
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ornek9
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)


{
int t1,t2,t3,i,n;
t1 = t2 = 1;
n = Convert.ToInt32(textBox1.Text);
listBox1.Items.Clear();
listBox1.Items.Add(Convert.ToInt32(t1));
listBox1.Items.Add(Convert.ToInt32(t2));
for (i = 1; i <= n - 2; i++)
{
t3 = t1 + t2;
listBox1.Items.Add(Convert.ToInt32(t3));
t1 = t2; t2 = t3;
}
}
}
}

while

Ornek10
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ornek10
{ public partial class Form1 : Form
{ public Form1()
{ InitializeComponent(); }

private void button1_Click(object sender, EventArgs e)


{ int i=1, n;
double c = 1d;
n = Convert.ToInt32(textBox1.Text);
while (i <= n)
{ c *= i; i++; }
textBox2.Text = Convert.ToString(c);
}
}
}

Prof. Dr. Fahri VATANSEVER

You might also like