- Visual Studio For Mac Save Git Credentials
- Visual Studio For Mac
- Mac Visual Studio Code
- Visual Studio For Mac Professional
Because Microsoft's Visual Studio only runs in the Windows environment, however, it is impossible to use Visual C++ directly on a Macintosh. We should point out that by installing VirtualPC, a Macintosh user may create a Windows simulator on their machine and thereby run Microsoft Visual C++. Visual Studio for Mac cannot save Unity C# script after edited it. I use Visual Studio for Mac to edit my script of Unity project, when I open my script in Unity, Visual Studio for Mac is open, I edit my script within it, but when I finished it, it cannot be saved.
-->Visual Studio for Mac provides a full-featured Integrated Development Environment (IDE) for developing .NET Core applications. This topic walks you through building a .NET Core solution that includes a reusable library and unit testing.
This tutorial shows you how to create an application that accepts a search word and a string of text from the user, counts the number of times the search word appears in the string using a method in a class library, and returns the result to the user. The solution also includes unit testing for the class library as an introduction to unit testing concepts. If you prefer to proceed through the tutorial with a complete sample, download the sample solution. For download instructions, see Samples and Tutorials.
Note
Your feedback is highly valued. There are two ways you can provide feedback to the development team on Visual Studio for Mac:
- In Visual Studio for Mac, select Help > Report a Problem from the menu or Report a Problem from the Welcome screen, which opens a window for filing a bug report. You can track your feedback in the Developer Community portal.
- To make a suggestion, select Help > Provide a Suggestion from the menu or Provide a Suggestion from the Welcome screen, which takes you to the Visual Studio for Mac Developer Community webpage.
Prerequisites
- OpenSSL (if running .NET Core 1.1): See the Prerequisites for .NET Core on Mac topic.
For more information on prerequisites, see the Prerequisites for .NET Core on Mac. For the full system requirements of Visual Studio 2017 for Mac, see Visual Studio 2017 for Mac Product Family System Requirements.
Building a library
- On the Welcome screen, select New Project. In the New Project dialog under the .NET Core node, select the .NET Standard Library template. This creates a .NET Standard library that targets .NET Core as well as any other .NET implementation that supports version 2.0 of the .NET Standard. Select Next.
- Name the project 'TextUtils' (a short name for 'Text Utilities') and the solution 'WordCounter'. Leave Create a project directory within the solution directory checked. Select Create.
- In the Solution sidebar, expand the
TextUtils
node to reveal the class file provided by the template, Class1.cs. Right-click the file, select Rename from the context menu, and rename the file to WordCount.cs. Open the file and replace the contents with the following code: - Save the file by using any of three different methods: use the keyboard shortcut ⌘+s, select File > Save from the menu, or right-click on the file's tab and select Save from the contextual menu. The following image shows the IDE window:
- Select Errors in the margin at the bottom of the IDE window to open the Errors panel. Select the Build Output button.
- Select Build > Build All from the menu.The solution builds. The build output panel shows that the build is successful.
Creating a test project
Unit tests provide automated software testing during your development and publishing. The testing framework that you use in this tutorial is xUnit (version 2.2.0 or later), which is installed automatically when the xUnit test project is added to the solution in the following steps:
- In the Solution sidebar, right-click the
WordCounter
solution and select Add > Add New Project. - In the New Project dialog, select Tests from the .NET Core node. Select the xUnit Test Project followed by Next.
- Name the new project 'TestLibrary' and select Create.
- In order for the test library to work with the
WordCount
class, add a reference to theTextUtils
project. In the Solution sidebar, right-click Dependencies under TestLibrary. Select Edit References from the context menu. - In the Edit References dialog, select the TextUtils project on the Projects tab. Select OK.
- In the TestLibrary project, rename the UnitTest1.cs file to TextUtilsTests.cs.
- Open the file and replace the code with the following:The following image shows the IDE with the unit test code in place. Pay attention to the
Assert.NotEqual
statement.It's important to make a new test fail once to confirm its testing logic is correct. The method passes in the name 'Jack' (uppercase) and a string with 'Jack' and 'jack' (uppercase and lowercase). If theGetWordCount
method is working properly, it returns a count of two instances of the search word. In order to fail this test on purpose, you first implement the test asserting that two instances of the search word 'Jack' aren't returned by theGetWordCount
method. Continue to the next step to fail the test on purpose. - Open the Unit Tests panel on the right side of the screen.
- Click the Dock icon to keep the panel open.
- Click the Run All button.The test fails, which is the correct result. The test method asserts that two instances of the
inputString
, 'Jack,' aren't returned from the string 'Jack jack' provided to theGetWordCount
method. Since word casing was factored out in theGetWordCount
method, two instances are returned. The assertion that 2 is not equal to 2 fails. This is the correct outcome, and the logic of our test is good. - Modify the
IgnoreCasing
test method by changingAssert.NotEqual
toAssert.Equal
. Save the file by using the keyboard shortcut ⌘+s, File > Save from the menu, or right-clicking on the file's tab and selecting Save from the context menu.You expect that thesearchWord
'Jack' returns two instances withinputString
'Jack jack' passed intoGetWordCount
. Run the test again by clicking the Run Tests button in the Unit Tests panel or the Rerun Tests button in the Test Results panel at the bottom of the screen. The test passes. There are two instances of 'Jack' in the string 'Jack jack' (ignoring casing), and the test assertion istrue
. - Testing individual return values with a
Fact
is only the beginning of what you can do with unit testing. Another powerful technique allows you to test several values at once using aTheory
. Add the following method to yourTextUtils_GetWordCountShould
class. You have two methods in the class after you add this method:TheCountInstancesCorrectly
checks that theGetWordCount
method counts correctly. TheInlineData
provides a count, a search word, and an input string to check. The test method runs once for each line of data. Note once again that you're asserting a failure first by usingAssert.NotEqual
, even when you know that the counts in the data are correct and that the values match the counts returned by theGetWordCount
method. Performing the step of failing the test on purpose might seem like a waste of time at first, but checking the logic of the test by failing it first is an important check on the logic of your tests. When you come across a test method that passes when you expect it to fail, you've found a bug in the logic of the test. It's worth the effort to take this step every time you create a test method. - Save the file and run the tests again. The casing test passes but the three count tests fail. This is exactly what you expect to happen.
- Modify the
CountInstancesCorrectly
test method by changingAssert.NotEqual
toAssert.Equal
. Save the file. Run the tests again. All tests pass.
Adding a console app
- In the Solution sidebar, right-click the
WordCounter
solution. Add a new Console Application project by selecting the template from the .NET Core > App templates. Select Next. Name the project WordCounterApp. Select Create to create the project in the solution. - In the Solutions sidebar, right-click the Dependencies node of the new WordCounterApp project. In the Edit References dialog, check TextUtils and select OK.
- Open the Program.cs file. Replace the code with the following:
- To run the app in a console window instead of the IDE, right-click the
WordCounterApp
project, select Options, and open the Default node under Configurations. Check the box for Run on external console. Leave the Pause console output option checked. This setting causes the app to spawn in a console window so that you can type input for theConsole.ReadLine
statements. If you leave the app to run in the IDE, you can only see the output ofConsole.WriteLine
statements.Console.ReadLine
statements do not work in the IDE's Application Output panel. - Because the current version of Visual Studio for Mac cannot run the tests when the solution is run, you run the console app directly. Right-click on the
WordCounterApp
project and select Run item from the context menu. If you attempt to run the app with the Play button, the test runner and app fail to run. For more information on the status of the work on this issue, see xunit/xamarinstudio.xunit (#60). When you run the app, provide values for the search word and input string at the prompts in the console window. The app indicates the number of times the search word appears in the string. - The last feature to explore is debugging with Visual Studio for Mac. Set a breakpoint on the
Console.WriteLine
statement: Select in the left margin of line 23, and you see a red circle appear next to the line of code. Alternatively, select anywhere on the line of code and select Run > Toggle Breakpoint from the menu. - Right-click the
WordCounterApp
project. Select Start Debugging item from the context menu. When the app runs, enter the search word 'cat' and 'The dog chased the cat, but the cat escaped.' for the string to search. When theConsole.WriteLine
statement is reached, program execution halts before the statement is executed. In the Locals tab, you can see thesearchWord
,inputString
,wordCount
, andpluralChar
values. - In the Immediate pane, type 'wordCount = 999;' and press Enter. This assigns a nonsense value of 999 to the
wordCount
variable showing that you can replace variable values while debugging. - In the toolbar, click the continue arrow. Look at the output in the console window. It reports the incorrect value of 999 that you set when you were debugging the app.
See also
As a .NET developer, I’ve spent most of my time coding on Windows machines. It’s only logical: Visual Studio is the richest development experience for building C# and VB.NET applications, and it only runs on Windows…right?
When I joined Stormpath to work on our open-source .NET authentication library, I was handed a MacBook Pro and given an interesting challenge: can a Mac be an awesome .NET development platform?
To my surprise, the answer is yes! I’ll share how I turned a MacBook Pro into the ultimate Visual Studio development machine.
How to Run Visual Studio on a Mac
Visual Studio doesn’t run natively on OS X, so my first step was to get Windows running on my MacBook Pro. (If you want an editor that does run natively, Xamarin Studio or Visual Studio Code might fit the bill).
There are multiple options for running Windows on a Mac. Every Mac comes with Apple’s Boot Camp software, which helps you install Windows into a separate partition. To switch between OSes, you need to restart.
Parallels is a different animal: it runs Windows (or another guest OS) inside a virtual machine. This is convenient because you don’t have to restart your computer to switch over to Windows. Instead, Windows runs in an OS X application window.
I found that a combination of both worked best for me. I installed Windows into a Boot Camp partition first, and then turned that partition into an active Parallels virtual machine. This way, I have the option of using Windows in the virtual machine, or restarting to run Windows natively at full speed.
I was initially skeptical of the performance of a heavy application like Visual Studio running in a virtual machine. The option to restart to Windows via Boot Camp gave me a fallback in case Visual Studio was sluggish.
There are some minor disadvantages to this method: you can’t pause the virtual machine or save it to a snapshot. A non-Boot Camp virtual machine doesn’t have these limitations. This guide will work regardless of what type of virtual machine you create.
After three months of serious use, and some tweaks, I’ve been very impressed with Parallels’ performance. I haven’t needed to boot directly to Windows at all. (For comparison, my host machine is a 15” mid-2015 MacBook Pro with 16GB of RAM and a 1TB flash drive.)
In the remainder of this guide, I’ll detail the steps I took to optimize both Parallels and Visual Studio to run at peak performance.
Installing Windows With Boot Camp and Parallels
This part’s easy. I followed Apple’s Boot Camp guide to install Windows in a separate partition.
Then, I installed Parallels and followed the Parallels Boot Camp guide to create a new virtual machine from the existing Boot Camp partition.
Tweaking Parallels for Performance and Usability
Visual Studio For Mac Save Git Credentials
The Parallels team publishes guidelines on how to maximize the performance of your virtual machine. Here’s what I adopted:
Virtual machine settings:
- 2 virtual CPUs
- 4096MB system memory
- 256MB graphics memory
Parallels options:
Visual Studio For Mac
- Optimization: Faster virtual machine, Adaptive hypervisor, Tune Windows for speed all turned on.
- Sharing: Shared cloud, SmartMount, and Access Windows folders from Mac turned off, as I didn’t need these for my workflow.
I experimented with both of Parallels’ presentation modes, Coherence and Full Screen. While it was cool to see my Windows apps side-by-side with OS X in Coherence mode, I found that the UI responsiveness (especially opening and closing windows and dialogs) felt sluggish.
Because of this, I use Full Screen exclusively now. I have Windows full-screen on my external Thunderbolt display, and OS X on my laptop. If I need to use OS X on my large monitor, I can swipe the Magic Mouse to switch desktops.
Adjusting OS X and Windows Features
I fixed a few annoyances and performance drains right off the bat:
- Function keys. If you’re using the Mac keyboard, you’ll want to change the function key behavior so the F1-F12 keys work correctly in Visual Studio. From System Preferences – Keyboard, make sure Use all F1, F2, etc. keys as standard function keys is checked. With this turned on, hold Fn to use the Mac functions (brightness, volume, etc.) on F1-F12. With an external non-Mac keyboard, this isn’t an issue.
- Start menu. I’m using Windows 8, and the removal of the Start menu annoyed me. I clung to my old ways and installed Start8 to restore it.
- Disable Windows visual effects. I turned off most of the Windows desktop manager visual effects by going to Control Panel – System and Security – Advanced system settings – Advanced – Performance – Settings – Visual Effects and choosing Adjust for best performance. However, I left Smooth edges of screen fonts checked because it improves text rendering on my monitor.
Installing Visual Studio and Helpful Extensions
Installing Visual Studio is a piece of cake once the virtual machine is set up. I simply downloaded the latest release from MSDN and let the installer run.
If you use an Apple Magic Mouse (as I do), Visual Studio tends to be overly eager to zoom the text size in and out as you swipe your finger over the mouse. The Disable Mouse Wheel Zoom add-on fixes this annoyance.
Improving Visual Studio for Performance
I was impressed with how well Visual Studio performed under emulation. With a large multi-project solution open, though, I saw some slowdowns.
Through trial and error, I found a number of things that could be disabled to improve performance. You may not want to make all of the changes I did, so pick and choose your own list of tweaks:
- Disable hardware-accelerated rendering. Unchecking Automatically adjust visual experience based on client performance, Enable rich client visual experience, and Use hardware graphics acceleration if available via Options – Environment made the UI feel much more responsive on my machine.
- Start up to an empty environment. Starting up Visual Studio for the first time feels a lot snappier if you skip the default news page on startup. Select Empty environment under Options – Environment – Startup – At startup.
- Remove unused extensions. Visual Studio ships with a number of extensions that you may not need. From Tools – Extensions and Updates – Installed, remove any extensions you aren’t actively using (you can always reinstall them later). I got rid of six extensions I didn’t need.
- Disable extra debugging features. I turned off both Enable Diagnostic Tools while debugging and Show elapsed time PerfTip while debugging in Options – Debugging – General. I wasn’t using these debugging features, and debugging felt snappier after I disabled them.
- Turn off the Navigation Bar. I found the code editor Navigation Bar to be unnecessary if the Solution Explorer is open. I disabled it via Options – Text Editor – All Languages – Navigation Bar.
- Disable CodeLens. CodeLens is a cool feature for collaboration, but it’s not part of my current workflow. I got rid of the CPU overhead by turning it off via Options – Text Editor – All
Languages – CodeLens – Enable CodeLens. - Turn off Track Changes. When a file is open in the code editor, Visual Studio will represent recent changes by displaying small regions of green or yellow on the scroll bar. If you can live without this, turn off Track changes via Options – Text Editor – General for a small performance boost.
- Turn off Track Active Item. Squeeze out a little bit more UI performance out by ensuring Track Active Item in Solution Explorer is unchecked under Options – Projects and Solutions – General.
Visual Studio on a Mac: The Best of Both Worlds
With these tweaks, I’ve come to love using Visual Studio on a Mac. The performance is good, and by running Windows in a virtual machine, I get the best of both OS worlds.
Mac Visual Studio Code
Want to see what I’m building with this setup? Check out our open-source .NET SDK on Github.
Visual Studio For Mac Professional
Do you have any other tricks you’ve used to improve Visual Studio performance? Any must-have add-ons that boost your productivity? Leave me a comment below!