Metotlarda params kullanımı — Csharp
Metotlarda params kullanımı — Csharp
Not: Toplama işlemi metot kullanılarak yapılmış.textbox’a araya virgül koyarak yazdığınız tüm sayıları toplarak sonucu label’e yazıyor.
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing;//www.gorselprogramlama.com/ using System.Linq; using System.Text; using System.Windows.Forms; namespace Yordamlarda_params_kullanımı { public partial class Form1 : Form { public Form1() { InitializeComponent(); } string[] sayilar; int[] sayi; private void btnTopla_Click(object sender, EventArgs e) { sayilar = txtSayilar.Text.Split(','); sayi = new int[sayilar.Length]; for (int i = 0; i < sayilar.Length; i++) { sayi[i] = Convert.ToInt32(sayilar[i]); } if (sayilar.Length == 1) { topla(sayi[0]); } else if (sayilar.Length == 2) { topla(sayi[0], sayi[1]); } else { for (int i = 0; i < sayilar.Length ; i++) { topla(sayi[0], sayi);//www.gorselprogramlama.com/ } } } void topla(int sabit , params int [] sayi) { int toplam = 0; if (sayi.Length == 0) { for (int i = 1; i <= sabit; i++) { toplam += i; } } else if (sayi.Length == 1) { for (int i = sayi[0]; i <= sabit; i++) { toplam += i; } } else { for (int i = 0; i < sayi.Length; i++) { toplam += sayi[i]; } } lblSonuc.Text = "Sonuç : " + toplam.ToString(); } //www.gorselprogramlama.com/ } }