Why does Visual Studio not complain when I write Console.WriteLine? - c#

I was recently watching tutorials on C# where they mentioned that specifying either using System and then using Console.WriteLine or writing System.Console.WriteLine if you don't specify using <namespace> was necessary for proper namespace resolution to happen when writing programs otherwise the compilation would fail.
But when I'm using Visual Studio 2022, Console.WriteLine seems to work without specifying any namespace at all.
Is this some optimisation that Visual Studio 2022 does? Why is this working?
On top of this, if I use System.Console.WriteLine, VS 2022 also greys out System and says "name can be simplified".

Try this
using System;
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}

If you create c# project of asp.netframrwork. It will automatically generate the "using System" namespace.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApp2
{
internal class Program
{
static void Main(string[] args)
{
}
}
}
If your console project is .NET 6 and above. Please refer to Exploring Ideas to Build Code While Learning Using Top-Level Statements.
Hope it helps you.

Related

Console dialog missing during debug in Visual Studio 2017

I recently started working with Visual Studio 2017. Jumping from 2010.
I created a new, very basic, console application and I am trying to test. The code runs fine but I am not seeing a console dialog anywhere (I have breakpoints and also a Console.Read()). I checked 'behind' all the windows. See nothing in the task tray.
Any ideas? Any new settings that I am not aware of?
Here is all of the code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleTest2
{
class Program
{
static void Main(string[] args)
{
Console.Read();
}
}
}
Definitely a console application.
Project Info

Sikuli error in c# with Sikuli integrator package

When I run this code:
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using Sikuli4Net.sikuli_REST;
using Sikuli4Net.sikuli_UTIL;
using SikuliModule;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Sikuli
{
class Program
{
static void Main(string[] args)
{
driver = new ChromeDriver();
driver.Manage().Window.Maximize();
driver.Navigate().GoToUrl("https://www.google.co.nz/");
Thread.Sleep(2000);
String image = #"C:\Users\safa\Desktop\gmail.jpg";
SikuliAction.Click(image);
}
}
}
I got this exception:
System.ComponentModel.Win32Exception:
'The system cannot find the file specified'
I changed file location but again I got same error.
This the image that I want to click on:
Run the visual studio as administrator using below steps
Click on start(windows) button
Type Visual Studio
Mouse Right Click on Visual Studio
Click on Run as Administrator option
I think there is no mistakes in code. So try this once.
Increase the wait time to 5000ms
For this program you don't need Sikuli4Net, remove usings related to that

MS C# tutorial code doesn't work in VS2013?

I'm following through Microsoft's C# tutorial to eventually learn .net, I've put this code into my VS 2013 and for some reason the output shows that "$" was an unexpected character. Throughout the tutorial it hasn't mentioned using anything side from System,
From MS
using System;
class Program
{
static void Main()
{
var name = "Steve"; // use your name here
Console.WriteLine($"Hello {name}!");
}
}
What I have
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main()
{
var name = "John";
Console.WriteLine($"Hello {name}!");
}
}
}
VS2013 says it was expecting a ')' in the part of the line where its written ($"Hello {name}!");
If I remove the $ it outputs the sentence as it's written Hello {name}!
What could the issue be?
Interpolated Strings (string literals starting with $") were introduced in C# 6.0 (Visual Studio 2015).
Note that you can download and use the free Community Edition of Visual Studio 2015, if you are student, an open-source developer or and individual developer. Visual Studio 2017 is Launching on March 7 2017 and also has a free Community Edition and supports C#7.0 (introducing tuples with the new tuple syntax, pattern matching and much more).
Official Visual Studio Downloads site.
This is how you do it the old way (pre c# 6.0).
Console.WriteLine(String.Format("Hello {0}", name));

Where are the `HttpContext.IsWebSocketRequest` property and the `HttpContext.AcceptWebSocketRequest` method?

I'm trying to get a simple "Hello World" type WebSocket server running on my Mac using Visual studio for Mac OS X.
Where are the HttpContext.IsWebSocketRequest property and the
HttpContext.AcceptWebSocketRequest method? The documentation appears to say they are in HttpContext inside the System.Web dll but I've referenced that (as well as System.Net) and Visual studio can't find them.
Is there something I'm missing or have forgotten?
This is the code I've that's giving me problems.
using System;
using System.Web;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Net.WebSockets;
using System.Collections.Generic;
public void ProcessRequest(HttpContext context)
{
if (context.IsWebSocketRequest)
context.AcceptWebSocketRequest(HandleWebSocket);
else
context.Response.StatusCode = 400;
}
The sample I'm following is: https://github.com/paulbatum/WebSocket-Samples/blob/master/AspNetWebSocketEcho/EchoHandler.ashx.cs
As far as I remember is not supported yet: http://go-mono.com/status/status.aspx?reference=4.5&profile=4.5&assembly=System
You can use one of the many third-party components available for Mono. I develop and maintain one of them https://github.com/vtortola/WebSocketListener

How to write test case using Appium in C#?

I want to use Appium to make automated testing for my Android app. I downloaded the Appium for Windows and could run my app on device from within appium.exe. Now I want to write test cases in C# which make use of selendroid integrated in Appium. I googled a lot but couldn't find any example demonstrating the same. I found one project on github but it's giving a lot of compiler errors. Could anybody guide me about how to write test cases using Appium in C#? If you have any resources, please provide me the same. Thanks.
You can go with installing Visual Studio 2012 or visual express for web. Use NUnit framework to write the test cases and execute them as a class library project in that.
1) Create a class library project and create a new class e.g "Class1". Add the packages as provided in the links above. Try the below code
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using OpenQA.Selenium;
using OpenQA.Selenium.Remote;
using NUnit.Framework;
using OpenQA.Selenium.Interactions;
using System.Threading;
using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Appium.MultiTouch;
using OpenQA.Selenium.Appium.Interfaces;
using System.Drawing;
namespace ClassLibrary2
{
[TestFixture]
public class Class1
{
public AppiumDriver driver;
public DesiredCapabilities capabilities;
public Class1()
{
Console.WriteLine("Connecting to Appium server");
capabilities = new DesiredCapabilities();
capabilities.SetCapability(CapabilityType.BrowserName, "Android");
capabilities.SetCapability(CapabilityType.Platform, "Windows");
capabilities.SetCapability(CapabilityType.Version ,"4.1.2");
capabilities.SetCapability("Device", "Android");
//Application path and configurations
driver = new AppiumDriver(new Uri("http://127.0.0.1:4723/wd/hub"), capabilities);
}
[Test]
public void login()
{
driver.FindElement(By.Name("Country")).Click();
//Your further code as per the application.
}
Then Build a solution for your project and run it in NUnit.

Categories