Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type Stopwatch could not be found

I'm learning C# using Xamarin Studio 4.0.3 (former MonoDevelop) on Windows 7. Trying using Stopwatch class (only piece of code)

using System.Diagnostics;

class MainClass {
    public static void Main (string[] args) {
         Stopwatch stopWatch = new Stopwatch();                 
    }
}

I get:

Error CS0246: The type or namespace name `Stopwatch' could not be found. Are you missing a using directive or an assembly reference?

My target framework is: Mono .NET 4.0. According to MSDN Stopwatch Class should be implemented.

BTW, DateTime class works fine.

Question is: am I missing something (right namespace, library linking) or Stopwatch simply isn't implemented?

like image 353
user2314351 Avatar asked Apr 24 '13 07:04

user2314351


4 Answers

Although you say that you've included the using directive, the error message begs to differ.

Ensure that this line is in the same .cs file as your method, at the very top:

using System.Diagnostics;

In Visual Studio, you can see this by clicking the icon underneath the error:

Maybe there's a similar feature in Xamarin Studio.

like image 107
Danny Beckett Avatar answered Sep 19 '22 05:09

Danny Beckett


I had a problem similar to this just recently in Xamarin studio for Mac. I was building a PLC project for a database that had to be shared between iOS, Android and Windows Store apps.

Check the PLC project files options - right click on the project header and select Options. Go to build targets and unselect Silverlight 5/Windows Phone.

If it is PLC, only a subset of the framework is available to you, as it only contains functionality which can be deployed across all devices selected. Hope this helps.

Regards Brett

like image 29
Thedood Avatar answered Sep 20 '22 05:09

Thedood


I recently ran into the same problem.

My issue was because I had created a new project, and was trying to use Stopwatch in the new project which had no references yet.

I right clicked my new projects reference folder -> Edit References, and added a reference to System.

Saved, built and all was well in the world.

like image 28
Des Horsley Avatar answered Sep 23 '22 05:09

Des Horsley


=> Stopwatch is defined in System (in System.dll) Assemblies,

=> Check if System.dll is included in References (If not added it can be added by right clicking on references in solution explorer )

=> Add the namespace using System.Diagnostics;

=> See more at https://msdn.microsoft.com/en-us/library/System.Diagnostics.Stopwatch(v=vs.110).aspx

like image 43
RotatingWheel Avatar answered Sep 23 '22 05:09

RotatingWheel