
یادگیری سی شارپ از مفاهیم پایه تا پروژه محور: شیگرایی، کار با SQL و LINQ، ORMها (Entity Framework)، ساخت پروژه مدیریت رستوران با گزارشات حرفهای و امکانات کامل!
مشاهده بیشتر
یادگیری MVC Core از مبانی تا پیشرفته: شیگرایی، Routing، Entity Framework، امنیت، تست یونیت، Razor، Ajax، و پروژههای کاربردی! یک دوره کامل برای تسلط بر توسعه وب با ASP.NET Core. به صورت حضوری و آنلاین!
مشاهده بیشترمشخصات مقاله
مثال دستور if (if...else example) سی شارپ
شکل کامل شده دستور if می تواند به همراه بخش else باشد. در سی شارپ چنانچه شرط درون پرانتز دستور if نتیجه نادرست داشته باشد(در نتیجه بلوک if اجرا نخواهد شد)، می توان بلوک خاصی برای این حالت با دستور else قرار داد تا اجرا شود. به طور کلی در آموزش سی شارپ در ساختارهای شرطی و تصمیم گیری چنانچه شرط مورد نظر نتیجه درست داشته باشد یک روند اجرا شده و در غیر این صورت می توان روند دیگری را اجرا نمود. استفاده از بخش else اختیاری است.
- به منظور استفاده از شرط if...else فایل ElectronicStore.cs را به ترتیب زیر اصلاح کنید.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | using System; using ElectronicStore2; public class Store { StoreItem CreateStoreItem() { int ? category = null ; decimal itemPrice = 0m; StoreItem sItem = new StoreItem(); Console.Title = "Electronic Super Store" ; Console.WriteLine( "=-= Nearson Electonics =-=" ); Console.WriteLine( "******* Store Items ******" ); Console.WriteLine( "To create a store item, enter its information" ); Console.Write( "Item Number: " ); sItem.itemNumber = long .Parse(Console.ReadLine()); Console.WriteLine( "Category" ); Console.WriteLine( "1. Unknown/Miscellaneous" ); Console.WriteLine( "2. Cables and Connectors" ); Console.WriteLine( "3. Cell Phones and Accessories" ); Console.WriteLine( "4. Headphones" ); Console.WriteLine( "5. Digital Cameras" ); Console.WriteLine( "6. PDAs and Accessories" ); Console.WriteLine( "7. Telephones and Accessories" ); Console.WriteLine( "8. TVs and Videos - Plasma / LCD" ); Console.WriteLine( "9. Surge Protector" ); Console.WriteLine( "10. Instructional and Tutorials (VHS & DVD)TVs and Videos" ); Console.Write( "Your Choice? " ); category = int .Parse(Console.ReadLine()); if (category == 1) sItem.category = ItemsCategories.Unknown; if (category == 2) sItem.category = ItemsCategories.CablesAndConnectors; if (category == 3) sItem.category = ItemsCategories.CellPhonesAndAccessories; if (category == 4) sItem.category = ItemsCategories.Headphones; if (category == 5) sItem.category = ItemsCategories.DigitalCameras; if (category == 6) sItem.category = ItemsCategories.PDAsAndAccessories; if (category == 7) sItem.category = ItemsCategories.TelephonesAndAccessories; if (category == 8) sItem.category = ItemsCategories.TVsAndVideos; if (category == 9) sItem.category = ItemsCategories.SurgeProtectors; if (category == 10) sItem.category = ItemsCategories.Instructional; Console.Write( "Make: " ); sItem.manufacturer = Console.ReadLine(); Console.Write( "Model: " ); sItem.model = Console.ReadLine(); Console.Write( "Unit Price: " ); itemPrice = decimal .Parse(Console.ReadLine()); if (itemPrice <= 0) sItem.unitPrice = 0.00m; else sItem.unitPrice = itemPrice; return sItem; } string GetItemCategory(ItemsCategories cat) { string strCategory = "Unknown" ; if (cat == ItemsCategories.CablesAndConnectors) strCategory = "Cables & Connectors" ; if (cat == ItemsCategories.CellPhonesAndAccessories) strCategory = "Cell Phones & Accessories" ; if (cat == ItemsCategories.Headphones) strCategory = "Headphones" ; if (cat == ItemsCategories.DigitalCameras) strCategory = "Digital Cameras" ; if (cat == ItemsCategories.PDAsAndAccessories) strCategory = "PDAs & Accessories" ; if (cat == ItemsCategories.TelephonesAndAccessories) strCategory = "Telephones & Accessories" ; if (cat == ItemsCategories.TVsAndVideos) strCategory = "TVs & Videos" ; if (cat == ItemsCategories.SurgeProtectors) strCategory = "Surge Protectors" ; if (cat == ItemsCategories.Instructional) strCategory = "Instructional" ; return strCategory; } void DescribeStoreItem(StoreItem item) { string strCategory = GetItemCategory(item.category); Console.Title = "Electronic Super Store" ; Console.WriteLine( "=-= Nearson Electonics =-=" ); Console.WriteLine( "******* Store Items ******" ); Console.WriteLine( "Store Item Description" ); Console.WriteLine( "Item Number: {0}" , item.itemNumber); Console.WriteLine( "Category: {0}" , strCategory); Console.WriteLine( "Make: {0}" , item.manufacturer); Console.WriteLine( "Model: {0}" , item.model); Console.WriteLine( "Unit Price: {0:C}" , item.unitPrice); } public static int Main() { Store st = new Store(); StoreItem saleItem = st.CreateStoreItem(); Console.Clear(); st.DescribeStoreItem(saleItem); System.Console.ReadKey(); return 0; } } <button></button> |
- برای اجرای برنامه, فهرست گزینه ی اصلی را باز کرده, سپس : Debug -> Start Debugging
- حال مقادیر را به ترتیب زیر وارد کنید و پس از هر کدام کلید Enter را فشار دهید.
=-= Nearson Electonics =-= ******* Store Items ****** To create a store item, enter its information Item Number: 992052 Category 1. Unknown/Miscellaneous 2. Cables and Connectors 3. Cell Phones and Accessories 4. Headphones 5. Digital Cameras 6. PDAs and Accessories 7. Telephones and Accessories 8. TVs and Videos - Plasma / LCD 9. Surge Protector 10. Instructional and Tutorials (VHS & DVD)TVs and Videos Your Choice? 4 Make: Sennheiser Model: HD-555 Unit Price: 104.00
- Enter را بزنید.
=-= Nearson Electonics =-= ******* Store Items ****** Store Item Description Item Number: 992052 Category: Headphones Make: Sennheiser Model: HD-555 Unit Price: $104.00
- اکنون پنجره ی DOS را بسته و به محیط برنامه نویسی برگردید.