{"id":222,"date":"2011-05-31T21:36:51","date_gmt":"2011-05-31T20:36:51","guid":{"rendered":"http:\/\/www.frozax.com\/blog\/?p=222"},"modified":"2015-04-04T20:18:48","modified_gmt":"2015-04-04T20:18:48","slug":"useful-c-visual-studio-tips","status":"publish","type":"post","link":"https:\/\/www.frozax.com\/blog\/2011\/05\/useful-c-visual-studio-tips\/","title":{"rendered":"Useful C# and Visual Studio tips you might not know"},"content":{"rendered":"<p>While working on my next game, I tried to keep notes of interesting tips I used. They are related to C# and Visual Studio.<\/p>\n<h3>Overriding ToString()<\/h3>\n<p>When debugging or prototyping, you often need to display complex objects on the screen on in the debugger output. A handy way to do this when dealing with complex classes is to override the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.object.tostring.aspx\">ToString() member<\/a>. Here is an example:<\/p>\n<blockquote>\n<pre>class ComplexClass\r\n{\r\n\u00a0 \u00a0int _number = 0;\r\n<span style=\"white-space: pre;\">   <\/span>string _string = \"My String\";\r\n\r\n\u00a0 \u00a0public ComplexClass( int n, string s )\r\n\u00a0 \u00a0{\r\n<span style=\"white-space: pre;\">      <\/span>_number = n;\r\n<span style=\"white-space: pre;\">      <\/span>_string = s;\r\n\u00a0 \u00a0}\r\n\r\n\u00a0 \u00a0public override string ToString()\r\n\u00a0 \u00a0{\r\n<span style=\"white-space: pre;\">      <\/span>return String.Format( \"{0} \/ {1}\", _number, _string );\r\n\u00a0 \u00a0}\r\n}<\/pre>\n<\/blockquote>\n<p>ToString() is also used by the debugger in the Watch dialog.<\/p>\n<h3>?? operator<\/h3>\n<p>I discovered the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms173224.aspx\">?? operator<\/a> a few months ago. I don&#8217;t use it much but could be useful in some cases. It allows you to replace the following code:<\/p>\n<blockquote>\n<pre>if( a != null )\r\n   b = a;\r\nelse\r\n   b = c;<\/pre>\n<\/blockquote>\n<p>by<\/p>\n<blockquote>\n<pre>b = a ?? c;<\/pre>\n<\/blockquote>\n<h3>New class template<\/h3>\n<p>When you create a new class (using the right-click on the project, Add, New Item&#8230;), the file created is not empty but already has a template and a few <em>using<\/em> statements. You can edit this template easily. For instance, I added a &#8220;<em>using fg;<\/em>&#8221; with my own game library to this template. The template is found in the Visual Studio directory: <strong>{Program Files}Microsoft Visual Studio 10.0Common7IDEVCSExpressItemTemplatesCache1033class.zip<\/strong>. There are templates for anything and you will probably want to edit a few of them.<\/p>\n<h3>new does not always allocates<\/h3>\n<p>In C#, even when using the new operator, you might not allocate memory. For instance, the following line:<\/p>\n<blockquote>\n<pre>Vector2 point = new Vector2( 10, 20 );<\/pre>\n<\/blockquote>\n<p>does not allocate memory, because <em>Vector2 <\/em>is not a class, but a structure. The rule is as simple as that: a class allocates memory (and returns a pointer) and a struct does not allocate memory. It&#8217;s simple, but\u00a0often misunderstood, especially if you come from C++.<\/p>\n<h3>Add As Link source file<\/h3>\n<p>Recently, I re-used source files from a previous project for a new one. I used the <em>Add Existing Item<\/em> option but Visual C# creates a copy of the source file in the current project directory instead of referencing the old file. I want to reference the old file so that it can be modified in both projects. To avoid that, you must select the hidden <em>Add As Link<\/em> option, by clicking the small arrow next to the <em>Add<\/em> button of the dialog box.<\/p>\n<p><a href=\"http:\/\/www.frozax.com\/blog\/wp-content\/uploads\/2011\/05\/add_as_link.png\"><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-678   alignnone\" title=\"add_as_link\" src=\"http:\/\/www.frozax.com\/blog\/wp-content\/uploads\/2011\/05\/add_as_link.png\" alt=\"add_as_link\" width=\"138\" height=\"88\" \/><\/a><\/p>\n<h3>Overloading the [] operator<\/h3>\n<p>I often have objects containing a list of items. For instance, in <a href=\"http:\/\/www.frozax.com\/games\/spring-up-harmony\">Spring Up Harmony<\/a>, I have a <em>Ball<\/em> Manager used to manage and draw the balls. I found it very handy to overload the <em>[] operator<\/em> to access elements of the list inside the manager from the outside directly.<\/p>\n<blockquote>\n<pre>List&lt;Ball&gt; _balls = new List&lt;Ball&gt;();\r\npublic int Count { get { return\u00a0_balls.Count; } }\r\npublic Ball this[int i] { get { return\u00a0_balls[i]; } }<\/pre>\n<\/blockquote>\n<h3>That&#8217;s it!<\/h3>\n<p>I hope you found some of the tips useful!<\/p>\n<p>Feel free to share more tips in the comments!<\/p>\n<p>Follow me on <a href=\"http:\/\/twitter.com\/Frozax\">twitter<\/a> or <a href=\"http:\/\/www.facebook.com\/pages\/Frozax-Games\/93984111894\">facebook<\/a> to get notified of new posts.<\/p>\n<div class=\"addtoany_share_save_container addtoany_content_bottom\"><div class=\"a2a_kit a2a_kit_size_32 addtoany_list a2a_target\" id=\"wpa2a_1\"><a class=\"a2a_button_twitter\" href=\"http:\/\/www.addtoany.com\/add_to\/twitter?linkurl=https%3A%2F%2Fwww.frozax.com%2Fblog%2F2011%2F05%2Fuseful-c-visual-studio-tips%2F&amp;linkname=Useful%20C%23%20and%20Visual%20Studio%20tips%20you%20might%20not%20know\" title=\"Twitter\" rel=\"nofollow\" target=\"_blank\"><\/a><a class=\"a2a_button_facebook\" href=\"http:\/\/www.addtoany.com\/add_to\/facebook?linkurl=https%3A%2F%2Fwww.frozax.com%2Fblog%2F2011%2F05%2Fuseful-c-visual-studio-tips%2F&amp;linkname=Useful%20C%23%20and%20Visual%20Studio%20tips%20you%20might%20not%20know\" title=\"Facebook\" rel=\"nofollow\" target=\"_blank\"><\/a><a class=\"a2a_button_google_plus\" href=\"http:\/\/www.addtoany.com\/add_to\/google_plus?linkurl=https%3A%2F%2Fwww.frozax.com%2Fblog%2F2011%2F05%2Fuseful-c-visual-studio-tips%2F&amp;linkname=Useful%20C%23%20and%20Visual%20Studio%20tips%20you%20might%20not%20know\" title=\"Google+\" rel=\"nofollow\" target=\"_blank\"><\/a><a class=\"a2a_button_reddit\" href=\"http:\/\/www.addtoany.com\/add_to\/reddit?linkurl=https%3A%2F%2Fwww.frozax.com%2Fblog%2F2011%2F05%2Fuseful-c-visual-studio-tips%2F&amp;linkname=Useful%20C%23%20and%20Visual%20Studio%20tips%20you%20might%20not%20know\" title=\"Reddit\" rel=\"nofollow\" target=\"_blank\"><\/a>\n<script type=\"text\/javascript\"><!--\nwpa2a.script_load();\n\/\/--><\/script>\n<\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>While working on my next game, I tried to keep notes of interesting tips I used. They are related to C# and Visual Studio. Overriding ToString() When debugging or prototyping, you often need to display complex objects on the screen on in the debugger output. A handy way to do this when dealing with complex&hellip; <a class=\"more-link\" href=\"https:\/\/www.frozax.com\/blog\/2011\/05\/useful-c-visual-studio-tips\/\">Continue reading <span class=\"screen-reader-text\">Useful C# and Visual Studio tips you might not know<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[12,21],"tags":[25,143,54,130,132,133,138],"_links":{"self":[{"href":"https:\/\/www.frozax.com\/blog\/wp-json\/wp\/v2\/posts\/222"}],"collection":[{"href":"https:\/\/www.frozax.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.frozax.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.frozax.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.frozax.com\/blog\/wp-json\/wp\/v2\/comments?post=222"}],"version-history":[{"count":2,"href":"https:\/\/www.frozax.com\/blog\/wp-json\/wp\/v2\/posts\/222\/revisions"}],"predecessor-version":[{"id":1693,"href":"https:\/\/www.frozax.com\/blog\/wp-json\/wp\/v2\/posts\/222\/revisions\/1693"}],"wp:attachment":[{"href":"https:\/\/www.frozax.com\/blog\/wp-json\/wp\/v2\/media?parent=222"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.frozax.com\/blog\/wp-json\/wp\/v2\/categories?post=222"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.frozax.com\/blog\/wp-json\/wp\/v2\/tags?post=222"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}