{"id":318,"date":"2024-08-07T13:21:11","date_gmt":"2024-08-07T07:21:11","guid":{"rendered":"https:\/\/www.texeltech.com\/?p=318"},"modified":"2024-08-07T13:29:17","modified_gmt":"2024-08-07T07:29:17","slug":"output-syntax-different-ways","status":"publish","type":"post","link":"http:\/\/www.texeltech.com\/?p=318","title":{"rendered":"Output syntax different ways"},"content":{"rendered":"\n<p class=\"has-medium-font-size\"><strong>Lets Create a Class of Product<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>using System;\nusing System.Collections.Generic;\nusing System.Linq;\nusing System.Text;\nusing System.Threading.Tasks;\n\nnamespace ConsoleApp1.Models\n{\n    public class Product\n    {\n        public string Name { get; set; }\n        public int Price { get; set; }\n        public string Unit { get; set; }\n        public string ProductType { get; set; }\n        public Product RelatedProduct { get; set; }\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Write the following code to program.cs file<\/strong><\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Product product = new Product()\n{\n    Name = \"Laptop\",\n    Price = 100000,\n    ProductType = \"Electronics\",\n    Unit = \"pcs\"\n};\n\nList&lt;Product> products = new List&lt;Product>() {\n\n    new Product()\n    {\n        Name = \"Mouse\",\n        Price  = 200,\n        ProductType = \"Electronics\",\n        Unit = \"pcs\"\n    },\n    new Product()\n    {\n        Name = \"Keyboard\",\n        Price  = 600,\n        ProductType = \"Computer\",\n        Unit = \"pcs\"\n    },\n    product\n};\n\nforeach (var item in products)\n{\n    \/\/ 1st way to output\n    \/\/Console.WriteLine(\"Name: \" + item.Name + \" Price: \" + item.Price + \" Related Product: \"+(item?.RelatedProduct?.Name?? \"N\/A\"));\n\n    \/\/ 2nd way to output\n    \/\/Console.WriteLine(string.Format(\"Name: {0} Price: {1} Related Product: {2}\", item.Name, item.Price, (item?.RelatedProduct?.Name ?? \"N\/A\")));\n\n    \/\/ 3rd way to output\n    string message = $\"Name: {item.Name} Price: {item.Price}, Related Product: {(item?.RelatedProduct?.Name ?? \"N\/A\")}\";\n    Console.WriteLine(message);\n\n    \/\/ 4th way to output\n    string message2 = $\"{nameof(item.Name)}: {item.Name} {nameof(item.Price)}: {item.Price}, {nameof(item.RelatedProduct)}: {(item?.RelatedProduct?.Name ?? \"N\/A\")}\";\n    Console.WriteLine(message2);\n}<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Lets Create a Class of Product Write the following code to program.cs file<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[38],"tags":[],"_links":{"self":[{"href":"http:\/\/www.texeltech.com\/index.php?rest_route=\/wp\/v2\/posts\/318"}],"collection":[{"href":"http:\/\/www.texeltech.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.texeltech.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.texeltech.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.texeltech.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=318"}],"version-history":[{"count":3,"href":"http:\/\/www.texeltech.com\/index.php?rest_route=\/wp\/v2\/posts\/318\/revisions"}],"predecessor-version":[{"id":321,"href":"http:\/\/www.texeltech.com\/index.php?rest_route=\/wp\/v2\/posts\/318\/revisions\/321"}],"wp:attachment":[{"href":"http:\/\/www.texeltech.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=318"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.texeltech.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=318"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.texeltech.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=318"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}