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

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));

Related

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

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.

Cryptsharp C# Visual Studio 2022 no definition

I've tried to run
string cryptedPassword = Crypter.MD5.Crypt(Passwort, new CrypterOptions()
{
{CrypterOption.Variant, MD5CrypterVariant.Apache}
});
from the cryptsharp documentation
But I don't seem to be able to run it.
Passwort is a random 5 char string
I have this code:
using System; using CryptSharp;
using System.IO;
using System.Security.Cryptography;
but keep running into errors:
Crypter has no definition for MD5
MD5CrypterVariant not available
What am I missing ?
Thanks in advance
If you want to use MD5 crypt algorithms, we need to install the following NuGet packages.
https://www.nuget.org/packages/CryptSharpOfficial/
Here is my code:
If there are still errors, you can try to reinstall the NuGet packages.

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

'ElasticTranscoder' does not exist in the namespace 'Amazon' (are you missing an assembly reference?)

I am stuck in a problem in which I am using Amazon SDK for .NET 4.5 on VS 2012
What my problem is that my code does not resolve ElasticTranscoder namespace
-I installed SDK 3 times all old to latest versions
-Checked on VS 2010 and 0n VS 2012
-Tried adding ref like 100 times again n again
Cleaing and rebuilding solutions
It never changes.
I am stuck for two days now.
Help me in finding the correct way to resolve this class AmazonElasticTranscoderClient
Anyone?
public void CreateJob(string Path, string bucket)
{
string accsessKey = CloudSettings.AccessKeyID;
string secretKey = CloudSettings.SecreteKey;
var etsClient = new **AmazonElasticTranscoderClient**(accsessKey,secretKey, RegionEndpoint.USEast1);
}
Hate to ask the obvious, but do you have these using statements at the top of your file:
using Amazon.ElasticTranscoder;
using Amazon.ElasticTranscoder.Model;
Also, try to create a brandnew project and pick the template:
Templates->Visual C#->AWS->App Services->Amazon Elastic Transcoder Sample
See if that installs and runs correctly - it does for me on VS2012 with .net 4.5

Microsoft.Office.Interop.Word reference not found

Recently, I have been escalated to work in a legacy project. This application is a plugin to Enterprise Architect that extracts the content to a .doc file. The point is, I tried to reference dll from different places, without success.
The error:
The type or namespace name 'Word' does not exist in the namespace 'Microsoft.Office.Interop' (are you missing an assembly reference?)
The places I have tried reference:
C:\Program Files (x86)\Microsoft Visual Studio 10.0\Visual Studio Tools for Office\PIA\Office14\Microsoft.Office.Interop.Word.dll
C:\Windows\assembly\GAC_MSIL\Microsoft.Office.Interop.Excel\15.0.0.0__71e9bce111e9429c\Microsoft.Office.Interop.Word.dll
I am using Vistual Studio 2010 and I have Office 2013 installed in my machine, but also tested in a machine with Office 2010.
Appreciate any help.
Update:
Everywhere in the code the word 'Word' is underlined and the compilation error is above.
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Word = Microsoft.Office.Interop.Word;
using Office = Microsoft.Office.Core;
namespace EATec.Helpers
{
/// <summary>
/// Classe que manipula documento Word via Interop
/// </summary>
public class MSWordHelper
{
private static object format = Microsoft.Office.Interop.Word.WdOpenFormat.wdOpenFormatAuto;
private static object wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue;
public static Microsoft.Office.Interop.Word.Document OpenDoc(object fileName)
{...
}
Legacy apps built against older versions of office may be doing something like (C++):
#import <msword9.olb> rename("ExitWindows","ExitWindowsWRD") rename "FindText","FindTextWRD") rename("ReplaceText","ReplaceTextWRD") rename("RGB","RGBWRD")
When I set up a new machine, I have a bat file to re-register the old files:
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\regtlibv12 "C:\Source\Office 97 dlls\mso9.dll"
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\regtlibv12 "C:\Source\Office 97 dlls\msword9.OLB"
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\regtlibv12 "C:\Source\Office 97 dlls\vbe6ext.OLB"
This has to be done when it's a newer version of Office installed on the PC.
Please note that regtlibv12 might not be on a windows 8 PC, and will need copying over from your old PC.
(Know this is nearly a year old, but just setting up a new PC at the moment...)

Categories