Tuple kullanımı etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster
Tuple kullanımı etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster

7 Temmuz 2012 Cumartesi

Tuple ile Metottan Geriye Birden Fazla Değer Döndürmek

.NET Framework ve C# 4.0’ın yayınlanması ile birlikte Tuple adında yeni bir tip ile tanıştık. Belki .NET tarafında bu tip ile yeni tanışıyoruz ama Python kullananlar için pek de yeni bir tip sayılmaz.

Matematikte ise, Tuple, belirli sayıda değerlerin sıralı listesi olarak geçer. Bu değerlere Tuple’ın bileşenleri denir. Örneğin, Ad, İkinci Ad ve Soyad’dan oluşan bir topluluk 3 elemanlı Tuple olarak nitelendirilebilir.

Bir örnek ile durumu daha net anlamaya çalışalım.

public Tuple<int, int> BolumVeKalanHesapla(int x, int y)
{
    return Tuple.Create(x/y, x%y);
}

public void TestMethod()
{
    var tuple = BolumVeKalanHesapla(10,3);
    Console.WriteLine("Bölüm: {0} Kalan: {1}", tuple.Item1, tuple.Item2);
}

Yukarıdaki örnekte, method, iki integer taşıyan bir Tuple döndürüyor. Yani, Tuple kullanarak, metottan geriye birden fazla değer döndürmüş oluyoruz. Böylece tembel programcılar daha az kod yazarak daha çok iş yapabilirler.

Tuple ile ilgili daha detaylı bilgi için aşağıdaki linkleri kullanabilirsiniz:

13 Kasım 2011 Pazar

Tuple: Örnek Kullanım

Tuples are commonly used in four ways:
  • To represent a single set of data. For example, a tuple can represent a database record, and its components can represent individual fields of the record.
  • To provide easy access to, and manipulation of, a data set.
  • To return multiple values from a method without using out parameters (in C#) or ByRef parameters (in Visual Basic).
  • To pass multiple values to a method through a single parameter. For example, the Thread.Start(Object) method has a single parameter that lets you supply one value to the method that the thread executes at startup time. If you supply a Tuple<T1, T2, T3> object as the method argument, you can supply the thread’s startup routine with three items of data.
The Tuple class does not itself represent a tuple. Instead, it is a factory class that provides static methods for creating instances of the tuple types that are supported by the .NET Framework. It provides helper methods that you can call to instantiate tuple objects without having to explicitly specify the type of each tuple component.

Örnek Kullanımlar:
var population = new Tuple<string, int, int, int, int, int, int>( "New York", 7891957, 7781984, 7894862, 7071639, 7322564, 8008278);

var population = Tuple.Create("New York", 7891957, 7781984, 7894862, 7071639, 7322564, 8008278);

Detaylar için aşağıdaki MSDN linkine bakabilirsiniz.
http://msdn.microsoft.com/en-us/library/system.tuple.aspx