コード例 #1
0
ファイル: Program.cs プロジェクト: hgunay/DotNetCore-Learning
        static void Main()
        {
            var customerService = new CustomerService();
            var customerData    = customerService.GetCustomerById(1);

            #region Statements

            // if statement
            if (customerData == null)
            {
                Console.WriteLine("Müşteri bilgileri bulunamadı.");
            }

            // if-else statement
            if (customerData != null)
            {
                Console.WriteLine($"{customerData.FullName()} - {customerData.EmailAddress}");
            }
            else
            {
                Console.WriteLine("Müşteri bilgileri bulunamadı.");
            }

            // if-else-if statement
            if (customerData != null && string.IsNullOrEmpty(customerData.FirstName))
            {
                Console.WriteLine($"{customerData.FullName()} - {customerData.EmailAddress}");
            }
            else if (customerData != null)
            {
                Console.WriteLine($"{customerData.FullName()} - {customerData.EmailAddress}");
            }
            else
            {
                Console.WriteLine("Müşteri bilgileri bulunamadı.");
            }

            // Nested if
            if (customerData != null)
            {
                if (string.IsNullOrEmpty(customerData.EmailAddress))
                {
                    Console.WriteLine($"{customerData.FullName()} - Müşteri e-posta bilgisi bulunamadı.");
                }
            }

            // switch statement
            switch (customerData.Id)
            {
            case 1:
                Console.WriteLine($"{customerData.FullName()} - {customerData.EmailAddress}");
                break;

            case 2:
                Console.WriteLine($"{customerData.FullName()} - {customerData.PhoneNumber}");
                break;

            default:
                Console.WriteLine(customerData.FullName());
                break;
            }

            // Nested switch statement
            switch (customerData.LastName)
            {
            case "Skywalker":
                Console.WriteLine("Last name : Skywalker");

                switch (customerData.FirstName)
                {
                case "Luke":
                    Console.WriteLine("I'm Luke Skywalker. I'm here to rescue you.");
                    break;

                case "Leia":
                    Console.WriteLine("Help me Obi-Wan Kenobi, you're my only hope.");
                    break;

                default:
                    Console.WriteLine("I'm your father!");
                    break;
                }

                break;

            case "Kenobi":
                Console.WriteLine("That's no moon. It's a space station.");
                break;

            default:
                Console.WriteLine("May the Force be with you.");
                break;
            }

            #endregion

            #region Loops

            // for loop
            for (var i = 1; i <= 3; i++)
            {
                Console.WriteLine($"for : {i}. {customerData.FullName()}");
            }

            // while loop
            var j = 1;
            while (j <= 3)
            {
                Console.WriteLine($"while : {j}. {customerData.FullName()}");
                j++;
            }

            // do...while loop
            var x = 1;
            do
            {
                Console.WriteLine($"do-while : {x}. {customerData.FullName()}");
                x++;
            } while (x <= 3);

            // foreach loop
            var list = new int[] { 1, 2, 3 };
            foreach (var item in list)
            {
                Console.WriteLine($"foreach : {item}. {customerData.FullName()}");
            }

            #endregion

            #region Jumping Statements

            // break
            var movieTypeList = new string[] { "dram", "action", "sci-fi", "romance" };
            foreach (var item in movieTypeList)
            {
                if (item == "sci-fi")
                {
                    Console.WriteLine($"Movie Type : {item}");
                    break;
                }

                Console.WriteLine($"Movie Type : {item}");
            }

            // continue
            movieTypeList = new string[] { "dram", "action", "romance", "sci-fi" };
            foreach (var item in movieTypeList)
            {
                if (item == "romance")
                {
                    continue;
                }

                Console.WriteLine($"Movie Type : {item}");
            }

            // goto
            movieTypeList = new string[] { "dram", "action", "romance", "sci-fi" };
            foreach (var item in movieTypeList)
            {
                switch (item)
                {
                case "dram":
                    goto default;

                case "action":
                    goto default;

                case "romance":
                    goto default;

                case "sci-fi":
                    Console.WriteLine($"Movie type is Sci-Fi");
                    break;

                default:
                    Console.WriteLine($"Movie type is...");
                    break;
                }
            }

            #endregion
        }
コード例 #2
0
        static void Main(string[] args)
        {
            #region try-catch örneği
            try
            {
                CustomerService customerService = null;
                var             customerData    = customerService.GetCustomerById(1);

                if (customerData == null)
                {
                    Console.WriteLine("Müşteri bilgileri bulunamadı.");
                }
                else
                {
                    Console.WriteLine($"{customerData.FullName()} - {customerData.EmailAddress}");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Hata oluştu! Hata : {ex}");
            }
            #endregion

            #region try-catch-finally örneği

            try
            {
                var customerService = new CustomerService();
                var customerData    = customerService.GetCustomerById(1);

                if (customerData == null)
                {
                    Console.WriteLine("Müşteri bilgileri bulunamadı.");
                }
                else
                {
                    Console.WriteLine($"{customerData.FullName()} - {customerData.EmailAddress}");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Hata oluştu! Hata : {ex}");
            }
            finally
            {
                Console.WriteLine("İşlem tamamlandı.");
            }

            #endregion

            #region throw örneği

            try
            {
                var customerService = new CustomerService();
                var customerData    = customerService.GetCustomerById(0);

                if (customerData == null)
                {
                    throw new NullReferenceException("Hata Kodu : 01 - NullRef hatası oluştu.");
                }
                else
                {
                    Console.WriteLine($"{customerData.FullName()} - {customerData.EmailAddress}");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Hata oluştu! Hata : {ex.Message}");
            }

            #endregion
        }
コード例 #3
0
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("Müşteri Bilgilerini Giriniz");
                Console.WriteLine("---------------------------");

                Console.Write("Müşteri Adı      : ");
                var firstName = Console.ReadLine();

                Console.Write("Müşteri Soyadı   : ");
                var lastName = Console.ReadLine();

                Console.Write("Telefon No       : ");
                var phoneNumber = Console.ReadLine();

                Console.Write("E-posta Adresi   : ");
                var email = Console.ReadLine();

                var customerService = new CustomerService();
                if (customerService != null)
                {
                    var customerId = customerService.AddCustomer(firstName, lastName, phoneNumber, email);

                    if (customerId > 0)
                    {
                        Console.WriteLine($"Müşteri No       : {customerId}");

                        Console.WriteLine();

                        Console.WriteLine("Adres Bilgilerini Giriniz");
                        Console.WriteLine("---------------------------");

                        Console.Write("Adres    : ");
                        var address = Console.ReadLine();

                        Console.Write("İl       : ");
                        var city = Console.ReadLine();

                        Console.Write("İlçe     : ");
                        var county = Console.ReadLine();

                        Console.Write("Semt     : ");
                        var district = Console.ReadLine();

                        var customerAddressId = customerService.AddCustomerAddress(customerId, address, city, county, district);

                        if (customerAddressId > 0)
                        {
                            var customer        = customerService.GetCustomerById(customerId);
                            var customerAddress = customerService.GetCustomerAddressById(customerId);

                            Console.WriteLine();

                            Console.WriteLine("Müşteri Bilgileri");
                            Console.WriteLine("-----------------");
                            Console.WriteLine($"Id : {customer.Id} | Adı - Soyadı : {customer.CustomerFullName} | Telefon No : {customer.PhoneNumber} | E-Posta : {customer.EmailAddress}");
                            Console.WriteLine("-----------------");

                            Console.WriteLine();

                            Console.WriteLine("Adres Bilgileri");
                            Console.WriteLine("---------------");
                            Console.WriteLine($"Id : {customerAddress.Id} | Müşteri Id : {customerAddress.CustomerId} | Adres : {customerAddress.Address} | İl : {customerAddress.City} | İlçe : {customerAddress.County} | Semt : {customerAddress.District}");
                            Console.WriteLine("---------------");

                            Console.WriteLine();

                            Console.WriteLine("Polymorphisim ile gelen bilgiler...");
                            Console.WriteLine("-----------------------------------");
                            customerService.GetCustomerInfo();
                            Console.WriteLine();
                            customerService.GetCustomerAddressInfo();
                        }
                    }
                    else
                    {
                        Console.WriteLine("Hata! Müşteri no alınamadı.");
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine();
                Console.WriteLine($"Hata oluştu! Hata : {ex}");
            }
            finally
            {
                Console.WriteLine();
                Console.WriteLine("İşlem tamamlandı.");
            }
        }