<p>I am looking to write some C# code for linux/windows/mac/any other platform, and am looking for best practices for portable code.</p>

<p>Project <a href="http://go-mono.org" rel="noreferrer">mono</a> has some great <a href="http://www.mono-project.com/Category:Porting" rel="noreferrer">porting</a> resources.</p>

<p>What are the best practices for portable C#?</p>

[{'answer_id': 42641, 'author': 'wvdschel', 'author_id': 2018, 'author_profile': 'https://Stackoverflow.com/users/2018', 'pm_score': 3, 'selected': False, 'text': "<p>Don't use Windows.Forms for GUIs, but Mono probably mentioned that already. Gtk# is much more consistent and reliable for cross platform GUIs.</p>\n"}, {'answer_id': 42645, 'author': 'Eric Z Beard', 'author_id': 1219, 'author_profile': 'https://Stackoverflow.com/users/1219', 'pm_score': 1, 'selected': False, 'text': "<p>If you want the code to be portable, you need to closely review the list of completed features on the Mono site.  They go into detail on each class in the framework, and the level of completeness.  You will have to take these things into consideration during the design process so that you don't go too far down a path and discover that a critical feature has not yet been implemented.</p>\n"}, {'answer_id': 42681, 'author': 'Orion Edwards', 'author_id': 234, 'author_profile': 'https://Stackoverflow.com/users/234', 'pm_score': 5, 'selected': True, 'text': "<p>I've actually used winforms and it was fine. It was BUTT UGLY, but it worked.</p>\n\n<p>Obviously, don't use P/Invoke, or any win32 stuff like the registry. Also be aware of any third party DLL's. For example, we use a third party SQLite dll which actually contains native code in it which we have to swap out if we want to run on OSX/linux.</p>\n"}, {'answer_id': 42746, 'author': 'Martin Salias', 'author_id': 4020, 'author_profile': 'https://Stackoverflow.com/users/4020', 'pm_score': 4, 'selected': False, 'text': '<p>I hate the term "Best practice" because it seems that some practices can be the best in any context, which is a risky thing, but I\'ll tell what I consider a "Good practice" for multi-platform code (and for most other type of development):</p>\n\n<p>Use a continuous integration engine and <strong>build for all the target platforms all the time</strong>.</p>\n\n<p>Sounds too complex? Well, if you really need to support multiple platforms, better to do it. No matter how careful you are with your code and library usage, if you test too late, you\'ll find yourself spending looong hours reworking big portions of the app.</p>\n'}, {'answer_id': 42758, 'author': 'Ubiguchi', 'author_id': 2562, 'author_profile': 'https://Stackoverflow.com/users/2562', 'pm_score': 2, 'selected': False, 'text': '<p>A few years ago I would have advised you to buy yourself a copy of my <a href="http://www.cross-platform.net/" rel="nofollow noreferrer">book</a> on cross-platform .NET, but as the book\'s somewhat out-of-date now you really need to stick to the info of the Mono site.</p>\n\n<p>The <a href="http://www.mono-project.com/Moma" rel="nofollow noreferrer">Mono Migration Analyzer (MoMA)</a> tool is pretty good for analyzing an exsisting .NET application and warning you of portability problems, but the best bet for new code is to use the latest stable version of Mono for your development work.</p>\n\n<p>As Orion said you need to be careful when using 3rd party DLLs, although my co-author wrote a <a href="http://www.tebiki.co.uk/cross-platform/downloads.html" rel="nofollow noreferrer">NativeProbe</a> tool to analyze DLLs for P/Invoke dependenecies if you do want to quickly check 3rd party software.</p>\n\n<p>If you are determined to develop on MS .NET then you should try and ensure you also build and unit test on Mono, and you should also watch out for a number of Windows specific namespaces such as the Microsoft.Win32 and System.Management namespaces.</p>\n'}, {'answer_id': 42782, 'author': 'Cory Foy', 'author_id': 4083, 'author_profile': 'https://Stackoverflow.com/users/4083', 'pm_score': 1, 'selected': False, 'text': '<p>There are some other simple things. Like don\'t assume path characters. Or <a href="http://www.cornetdesign.com/2006/03/importance-of-systemenvironmentnewline.html" rel="nofollow noreferrer">newlines</a>. </p>\n\n<p>I\'m one of the people who regularly compiles NUnit on Mono on Linux or OSX. </p>\n\n<p>Also, don\'t assume that the compilers work exactly the same. We\'ve found an issue recently where the MS C# compiler appears to be including things that the Mono one doesn\'t, requiring extra references in our build script. </p>\n\n<p>Other than that, it has been pretty straightforward. I remember the first time we got <a href="http://www.cornetdesign.com/2006/07/nunit-gui-running-green-on-monolinux.html" rel="nofollow noreferrer">the GUI running on Mono/Linux</a> - it was pretty exciting (even if it <em>was</em> pretty ugly)</p>\n'}, {'answer_id': 55946, 'author': 'Sam Warwick', 'author_id': 3873, 'author_profile': 'https://Stackoverflow.com/users/3873', 'pm_score': 4, 'selected': False, 'text': '<p>Watch out for anything to do with filename and path manipulation and make use of the portable .NET methods in <a href="http://msdn.microsoft.com/en-au/library/system.io.path.aspx" rel="nofollow noreferrer">System.IO.Path</a> ie.</p>\n\n<p>instead of:</p>\n\n<pre><code>string myfile = somepath + "\\\\file.txt";\n</code></pre>\n\n<p>do:</p>\n\n<pre><code>string myfile = Path.Combine(somepath, "file.txt");\n</code></pre>\n\n<p>If you need to specify a path separator then you would use <strong>Path.Separator</strong> etc</p>\n'}, {'answer_id': 170320, 'author': 'Andrei Rînea', 'author_id': 1796, 'author_profile': 'https://Stackoverflow.com/users/1796', 'pm_score': 3, 'selected': False, 'text': '<p>Don\'t use "\\r\\n" for a new line. Use <strong>Environment.NewLine</strong></p>\n\n<p>Remember : </p>\n\n<ul>\n<li>*NIX uses just the newline character ("\\n")</li>\n<li>Windows uses "\\r\\n"</li>\n<li>MacIntosh uses "\\r" (I am not really sure about this - feel free to correct me).</li>\n</ul>\n\n<p><em>L.E.: It seems that some newer MacOSes don\'t use the "\\r" line separator anymore.</em></p>\n'}, {'answer_id': 292102, 'author': 'Jacksonh', 'author_id': 6803, 'author_profile': 'https://Stackoverflow.com/users/6803', 'pm_score': 1, 'selected': False, 'text': '<p>One missing item: Make sure that file names are case sensitive. File.Open ("MyFile.txt"); isn\'t going to work on Unix if your file is named myfile.txt.</p>\n'}]
['https://Stackoverflow.com/questions/42627', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1583/']