DEV Community

Cover image for 2. Anonymous Types
MustafaSamedYeyin
MustafaSamedYeyin

Posted on

3 1

2. Anonymous Types

Öğrenilecekler : Anonymous Types

Anonymous Types ne işe yarar ?

Anonymous Type'lar nesnenin tipini belirtmeden tanımlama yapmanızı(declaration) sağlar. ayrıca bakınız

Örnek :

// Anonymous Types declaration(tanımlaması)
var v = new { Amount = 108, Message = "Hello" };

// Usage Example
Console.WriteLine(v.Amount + v.Message);
Enter fullscreen mode Exit fullscreen mode

Kullanım şekilleri, örnekleri :

1.) Çoğunlukla linq sorgularında select query'si kullanılır.

Örnek :

var productQuery =
    from prod in products
   select new { prod.Color, prod.Price }; // bu satıra dikkat

foreach (var v in productQuery)
{
    Console.WriteLine("Color={0}, Price={1}", v.Color, v.Price);
}
Enter fullscreen mode Exit fullscreen mode

2.) Örnek kullanım :

var anonArray = new[] { new { name = "apple", diam = 4 }, new { name = "grape", diam = 1 }};
Enter fullscreen mode Exit fullscreen mode

Yukarıda array tanımladık ve tipini belirtmedik.

3.) Diyelimlik tanımladığınız bir anonim tipin (anonymous type) daha sonra değerini değiştirmek istiyorsunuz. Ama bu mümkün değil (çünkü read only). Peki ne yapabiliriz ?

Örnek :

var apple = new { Item = "apples", Price = 1.35 };
var onSale = apple with { Price = 0.79 };
Console.WriteLine(apple);
Console.WriteLine(onSale);
Enter fullscreen mode Exit fullscreen mode

With diyerek asıl objenin (apple) değerlerinden price'ına yeni değer atayarak onSale objesine atıyoruz. Dikkat ederseniz apple objesinin değerinde bir değişiklik olmadı (çünkü zaten read only).

4.) Örnek bir kullanım daha, pratik yaparak anlamanız için yazıyorum:

var apple = new { Item = "apples", Price = 1.35 };
var onSale = apple with { Price = 0.79 };
Console.WriteLine(apple);
Console.WriteLine(onSale);
var anonArray = new[] { new { name = apple,versiyon="v1"}, new { name = onSale ,versiyon="v2"} };
Console.WriteLine(anonArray[0]);
Console.WriteLine(anonArray[1]);
Enter fullscreen mode Exit fullscreen mode

5.) Örnek bir kullanım daha, pratik yaparak anlamanız için yazıyorum :

List<string> list1 = new List<string>() { "a", "b", "c" };
List<string> list2 = new List<string>() { "c", "d", "e" };


var newList = new[] { list1, list2 }.ToList();
System.Console.WriteLine(newList[0][0]); //a
System.Console.WriteLine(newList[0][1]); //b
System.Console.WriteLine(newList[0][2]); //c
System.Console.WriteLine(newList[1][0]); //c
System.Console.WriteLine(newList[1][1]); //d
System.Console.WriteLine(newList[1][2]); //e
Enter fullscreen mode Exit fullscreen mode

ayrıca bakabilirsiniz

ayrıca okuyabilirsiniz

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →