static void Main(string[] args) { string productName = "apple"; double cost = 15; string description = "amasya apple"; string[] fruits = new string[] { "Apple" }; Product product1 = new Product(); product1.Name = "Apple"; product1.Cost = 15; product1.Description = "amasya apple"; Product product2 = new Product(); product2.Name = "Watermelon"; product2.Cost = 8; product2.Description = "diyarbakır watermelon"; Product[] products = new Product[] { product1, product2 }; foreach (Product product in products) { Console.WriteLine(product.Name); Console.WriteLine(product.Cost); Console.WriteLine(product.Description); Console.WriteLine("***********"); } Console.WriteLine("*********methods*******"); //instance //encapsulation CartManager cartmanager = new CartManager(); cartmanager.Add(product1); cartmanager.Add(product2); cartmanager.Sum2("Pear", "Green Pear", 12, 8); }
static void Main(string[] args) { Product product1 = new Product(); product1.Name = "Apple"; product1.Price = 15; product1.Description = "Amasya Apple"; Product product2 = new Product(); product2.Name = "Watermelon"; product2.Price = 80; product2.Description = "Diyarbakır Watermelon"; Product[] products = new Product[] { product1, product2 }; //The first "Product" is required because of the type-safe development language //The second "product" is an alias //The third "products" is the array of "products" foreach (Product product in products) { Console.WriteLine(product.Name); Console.WriteLine(product.Price); Console.WriteLine(product.Description); Console.WriteLine("----------------------------"); } Console.WriteLine("------------Methods-------------"); //Instance //Encapsulation CartManager cartManager = new CartManager(); cartManager.Add(product1); cartManager.Add(product2); cartManager.Add2("Pear", "Green Pear", 12, 10); cartManager.Add2("Apple", "Green Apple", 20, 15); cartManager.Add2("Watermelon", "Diyarbakır Watermelon", 50, 1); }
static void Main(string[] args) { Product product1 = new Product(); product1.Name = "Elma"; product1.Price = 15; product1.Description = "Amasya Elması"; Product product2 = new Product(); product2.Name = "Karpuz"; product2.Price = 80; product2.Description = "Diyarbakır karpuzu"; Product[] products = new Product[] { product1, product2 }; // type-safe foreach (var product in products) { Console.WriteLine(product.Name); Console.WriteLine(product.Price); Console.WriteLine(product.Description); Console.WriteLine("---------------"); } Console.WriteLine("---------- Methods ----------"); // Instance // Encapsulation CartManager cartManager = new CartManager(); cartManager.AddToCart(product1); cartManager.AddToCart(product2); cartManager.AddToCart2("Armut", "Yeşil Armut", 12, 10); cartManager.AddToCart2("Elma", "Yeşil Elma", 12, 9); cartManager.AddToCart2("Karpuz", "Diyarbakır Karpuzu", 12, 8); }
static void Main(string[] args) { Product product1 = new Product(); product1.Id = 1; product1.ProductName = "Elma"; product1.Price = 15; product1.Explanation = "Amasya Elması"; Product product2 = new Product(); product2.Id = 2; product2.ProductName = "Karpuz"; product2.Price = 80; product2.Explanation = "Diyarbakır Karpuzu"; Product[] products = new Product[] { product1, product2 }; foreach (var product in products) { Console.WriteLine(product.Id); Console.WriteLine(product.ProductName); Console.WriteLine(product.Price); Console.WriteLine(product.Explanation); Console.WriteLine("--------------"); } Console.WriteLine("--------Methods----------"); //Instance oluşturma "cartManager" //encapsulation** CartManager cartManager = new CartManager(); cartManager.Add(product1); cartManager.Add(product2); cartManager.Add2("Armut", "Yeşil armut", 12, 10); cartManager.Add2("Elma", "Yeşil elma", 12, 9); cartManager.Add2("Karpuz", "Diyarbakır karpuzu", 12, 8); }
static void Main(string[] args) { var product1 = new Product { Name = "Elma", Price = 15, Info = "Amasya Elması", Id = 1 }; var product2 = new Product { Name = "Karpuz", Price = 80, Info = "Diyarbakır Karpuzu", Id = 2 }; var products = new Product[] { product1, product2 }; foreach (var product in products) { Console.WriteLine(product.Name); Console.WriteLine(product.Price); Console.WriteLine(product.Info); Console.WriteLine(product.Id); Console.WriteLine("------------------"); } Console.WriteLine("----------Metotlar-----------"); Console.WriteLine("Sepete eklemek istediğiniz ürünün id'sini giriniz: "); var id = Convert.ToInt32(Console.ReadLine()); var cartManager = new CartManager(); var selectedProduct = products.FirstOrDefault(x => x.Id == id); cartManager.Add(selectedProduct); }
static void Main(string[] args) { //string[] products = new string[] { "Dell", "Asus" }; Product product1 = new Product(); product1.Id = new Guid(); product1.Name = "Apple"; product1.Price = 15; product1.StockQuantity = 100; Product product2 = new Product() { Id = new Guid(), Name = "Samsung", Price = 20, StockQuantity = 200 }; Product[] products = new Product[] { product1, product2 }; //type safe foreach (var product in products) { Console.WriteLine("Name:{0}\nPrice:{1}\n--------"); } //instance CartManager cartManager = new CartManager(); //encapsulation cartManager.Add(product1); cartManager.Add(product2); cartManager.Add2("Asus", "Rog", 200, 50); cartManager.Add2("Dell", "Inspiron", 150, 25); }
static void Main(string[] args) { Product prod1 = new Product(); prod1.Name = "Apple"; prod1.Price = 6; prod1.Description = "Amasya Apple"; Product prod2 = new Product(); prod2.Name = "Watermelone"; prod2.Price = 55; prod2.Description = "This is a watermelone.."; Product[] products = new Product[] { prod1, prod2 }; foreach (Product prod in products) { Console.WriteLine(prod.Name + " " + prod.Price + " " + prod.Description); } Console.WriteLine("\n-------------------Methods----------------------"); //instantiations CartManager cartManager = new CartManager(); cartManager.AddToCart(prod1); cartManager.AddToCart(prod2); Console.WriteLine("\n---------void methods------------"); //Method does not return a value Add1(10, 5); Console.WriteLine("\n-------------methods returning value-----------"); //Method returning value //sum2 value will be 15 after this method call //We can use var instead of int var sum2 = Add2(10, 5); Console.WriteLine("Add2 method call: 10 + 5 = " + sum2); //sum2 will be 30 after this method call, because the second parameter is not given and the method will use the //default value of the missing parameter which is 20 sum2 = Add2(10); Console.WriteLine("Add2 method call(20 is the default value of the second number): 10 + 20 = " + sum2); Console.WriteLine("\n-------------ref keyword with the methods-----------"); //ref keyword //Let we have two variables int number1 = 5; int number2 = 7; //now we pass values of these variables to our Add3 method. //the values of the variables above at the 53th and 54th lines are copied to the parameters of the method. //the assignment of "number1 = 50;" in the body (see line 98) of this method won't effect our variable of //number1 at the 53th line. int sum3 = Add3(number1, number2); Console.WriteLine("Add3: number1 out of the method: " + number1); //if we want to enable the operations to effect the value type variables which are out of the methods, //we add ref keyword to both definition and invocation of our method. int sum4 = Add4(ref number1, number2); Console.WriteLine("Add4: number1 out of the method: " + number1); Console.WriteLine("\n-------------out keyword with the methods-----------"); //out keyword works almost same as ref keyword but it has 2 differences, //ref keyword needs an initialized variable, which means the ref variable has to have an initial value. //out keyword doesn't need an initial value. Additionally it already has to assign a value to the given variable name. //for example, the code below is commented out because won't work: //int number1; //int number2 = 8; //sum4 = Add4(ref number1, number2); //But this usage with out is valid: int num1; int num2 = 3; int sum5 = Add5(out num1, num2); Console.WriteLine("Add5: num1 out of the method: " + num1); //By the way, the variable used with out, or ref keywords does not need to have same name as in the methods. int x = 7; int y = 19; sum5 = Add4(ref x, y); Console.WriteLine("Add4: x variable out of the method: " + x); int k; sum5 = Add5(out k, y); Console.WriteLine("Add5: k variable out of the method: " + k); Console.WriteLine("\n-------------method overloading-----------"); //We can define methods with the same name with different parameter count or types as long as C# can distinguish //which of the same name methods to invoke by considering the parameters. Console.WriteLine("Method with 2 parameters: " + Multiply(3, 9)); Console.WriteLine("Method with 3 parameters: " + Multiply(3, 7, 17)); Console.WriteLine("\n-------------methods with params keyword-----------"); //what if we want a method that returns sum of some arbitrary count of numbers? //method definition with params keyword allows us to invoke a method with flexible count of parameters. //params can be used with additional other parameters, in this case the keyword with params have to be the last one. //calculates and returns sum of the given values Console.WriteLine("Method with params usage: " + Total(5, 1, 2, 3, 4, 5, 6, 7, 8)); //calculates sum of the given values other than first one and multiplys sum by the first parameter which is x Console.WriteLine("Method with int x + params: " + Total2(5, 1, 2, 3, 4, 5, 6, 7, 8)); } /* ==========================================End of Main===========================================*/
static void Main(string[] args) { // Metodlar, tekrar tekrar kullanılabilirliği sağlayan kod bloklarıdır. // DRY principle: Do not repeat yourself! - Clean Code - Best Practice (Doğru Uygulama Teknikleri) // Örneğin, e-ticaret sitesinde ürünlerin sepete ekleme kısmı. // Sepete ekleme her yere eklenen hatta mail ile gönderilen aynı fonksiyonu, metodu içerir. // Class'tan bir değişken tanımlama: Product product1 = new Product(); // Class'ın örneği yani instance oluşturma product1.name = "Elma"; product1.price = 14.99; product1.description = "Amasya Elması"; Product product2 = new Product(); product2.name = "Portakal"; product2.price = 8.99; product2.description = "Antalya Portakalı"; Product product3 = new Product(); product3.name = "Karpuz"; product3.price = 80; product3.description = "Diyarbakır Karpuzu"; // Class'tan oluşturulan değişkenleri diziye toplamak: // Belirtilen tipte birden fazla data içerir. Product[] products = new Product[] { product1, product2, product3 }; foreach (Product product in products) {//products'ın içini gez, product gezilecek verilere takılan isim(alias), Product ise veri tipi (type-safety için) // var da yazılsa olur! Console.WriteLine(product.name + " " + product.price + " " + product.description); Console.WriteLine("------------------------------------"); } Console.WriteLine("\nFor Döngüsü Hatırlatma :)"); for (int i = 0; i < products.Length; i++) { Console.WriteLine(products[i].name + " " + products[i].price + " " + products[i].description); Console.WriteLine("------------------------------------"); } Console.WriteLine("--------------Metodlar--------------"); // Class instance - örnek // Burada sayfaların patlamadı! - Encapsulation // Encapsulation : kapsülleme, bir yapıyı bir kapsüle düzene koymaktır. CartManager cartManager = new CartManager(); cartManager.addToCart(product1); // çağrılan metod parametre ister cartManager.addToCart(product2); cartManager.addToCart(product3); // Class olmasaydı, böyle ürün gönderildi!! // Ürünün Stok Fiyatınında girilmesi istensin! // Hepsi sayfa olsaydı, her yer bozuldu, her ürünün stok fiyatını girmen gerekecek, tek tek değiştirmek gerekecek! cartManager.addAlternative("Armut", "Sarı Armut", 12, 10); cartManager.addAlternative("Elma", "Sarı Elma", 8, 10); cartManager.addAlternative("Karpuz", "Japon", 45, 10); // cw = snippet of print command --- Console.WriteLine(); // Metodlar, reusability sağlar. (Tekrar tekrar kullanılabilme özelliği verir!) }
static void Main(string[] args) { CartManager cartManager = new CartManager(); cartManager.Add(); }