{"id":1016,"date":"2010-05-21T09:08:10","date_gmt":"2010-05-21T09:08:10","guid":{"rendered":"http:\/\/www.codingfriends.com\/?p=1016"},"modified":"2010-05-21T09:08:10","modified_gmt":"2010-05-21T09:08:10","slug":"c-console-parameters-test","status":"publish","type":"post","link":"https:\/\/www.codingfriends.com\/index.php\/2010\/05\/21\/c-console-parameters-test\/","title":{"rendered":"C# &#8211; Console Parameters test"},"content":{"rendered":"<p>As from my previous post on <a href=\"http:\/\/www.codingfriends.com\/index.php\/2010\/05\/20\/console-parameters\/\">c++ console parameters<\/a>, I thought that I would do one with c# (c sharp) as well, just to show the difference in the languages as well.<\/p>\n<p>Compared to c++ where you cannot use the &#8220;==&#8221; (is equal to) operation because in c++ that is comparing two different memory locations and not the value within the left\/right hand variables.  Well in c# you can, there is a Equals method as well that you can use, but either or works fine, so in the example code below here is the main part, where I am comparing against a console parameter equalling -h (normally the help) <\/p>\n<pre lang=\"csharp\">\r\nif (args[i].Equals(\"-h\")) Console.WriteLine(\"-h option selected(equals)\");\r\nif (args[i]==\"-h\") Console.WriteLine(\"-h option selected (==)\");\r\n<\/pre>\n<p>both of them are fine to use as you can see from the output at the bottom of the post, both will print out that &#8220;-h option selected&#8221;, best to use the Equals though.<\/p>\n<p>Here is the full source code<\/p>\n<pre lang=\"csharp\">\r\nusing System;\r\n\r\nnamespace consoletest\r\n{\r\n\tclass MainClass\r\n\t{\r\n\t\tpublic static void Main (string[] args)\r\n\t\t{\r\n\t\t\tfor (int i = 0; i < args.Length; i++)\r\n\t\t\t{\r\n\t\t\t\tConsole.WriteLine(\"args \" + i + \" : \" + args[i]);\t\t\t\t\t\t\t\t\r\n\t\t\t\tif (args[i].Equals(\"-h\")) Console.WriteLine(\"-h option selected(equals)\");\r\n\t\t\t\tif (args[i]==\"-h\") Console.WriteLine(\"-h option selected (==)\");\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n<p>and here is the output using mono as the .Net runtime environment, as you can see both -h has been outputed<\/p>\n<pre lang=\"bash\">\r\nmono consoletest.exe -h here\r\nargs 0 : -h\r\n-h option selected(equals)\r\n-h option selected (==)\r\nargs 1 : here\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>As from my previous post on c++ console parameters, I thought that I would do one with c# (c sharp) as well, just to show the difference in the languages as well. Compared to c++ where you cannot use the &#8220;==&#8221; (is equal to) operation because in c++ that is comparing two different memory locations &hellip; <a href=\"https:\/\/www.codingfriends.com\/index.php\/2010\/05\/21\/c-console-parameters-test\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">C# &#8211; Console Parameters test<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[212],"class_list":["post-1016","post","type-post","status-publish","format-standard","hentry","category-c_sharp","tag-console-parameters"],"_links":{"self":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/1016","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/comments?post=1016"}],"version-history":[{"count":1,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/1016\/revisions"}],"predecessor-version":[{"id":1017,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/1016\/revisions\/1017"}],"wp:attachment":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/media?parent=1016"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/categories?post=1016"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/tags?post=1016"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}