Can't make any list in C# - c#

I can't figure out what is wrong. I run this code in Visual Studio in a C# .Net Console Project. Trying to make a simple list. I am using lists in more complicated code, but it is not working and now I can't get lists of any kind to run.
I get the error message on the line List<string> Mylist = new List<string>();
The type or namespace 'List<>' could not be found (are you missing a
using directive or an assembly reference?).
namespace Lists
{
class Program
{
static void Main()
{
List<string> Mylist = new List<string>();
Mylist.Add("a");
Mylist.Add("b");
Mylist.Add("c");
}
}
}

Include the proper namespace to access the types in it. List<T> is contained in the System.Collections.Generics namespace so you need to add the following line on top of your program:
using System.Collections.Generic;
If you don't know which namespace is missing simply press CTRL + . on the highlighted line and Visual Studio will add it automatically.

Related

Build.cs not working even after being restores to it's previous version

I'm trying to compile a project I'm working on and this error mesages pops out:
Invalidating makefile for SpaceShooterEditor (SpaceShooter.Build.cs modified)
While compiling E:\ue projects\SpaceShooter\Intermediate\Build\BuildRules\SpaceShooterModuleRules.dll:
e:\ue projects\SpaceShooter\Source\SpaceShooter\SpaceShooter.Build.cs(3,29) : error CS0246: The type or namespace name 'ModuleRules' could not be found (are you missing a using directive or an assembly reference?)
e:\ue projects\SpaceShooter\Source\SpaceShooter\SpaceShooter.Build.cs(5,25) : error CS0246: The type or namespace name 'ReadOnlyTargetRules' could not be found (are you missing a using directive or an assembly reference?)
ERROR: Unable to compile source files.
Any idea why this might happen? Last night i was trying to code a widget, modified the build.cs, then replaced the modified version (which chrashed the game) with a build.cs that worked previously, and still nothing? Is there any hope to make it work or should i start over? Moreover, how can this be avoided?
I already did the restarts and refreshes. I went to even delete the binaries and some cashed files and it didn't work.
Below you'll find the content of the Build.cs:
public class SpaceShooter : ModuleRules
{
public SpaceShooter(ReadOnlyTargetRules Target) : base(Target)
{
PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;
PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore" });
PrivateDependencyModuleNames.AddRange(new string[] { });
// Uncomment if you are using Slate UI
// PrivateDependencyModuleNames.AddRange(new string[] { "Slate", "SlateCore" });
// Uncomment if you are using online features
// PrivateDependencyModuleNames.Add("OnlineSubsystem");
// To include OnlineSubsystemSteam, add it to the plugins section in your uproject file with the Enabled attribute set to true
}
}
When i try to input specify the using UnrealBuildTool;, Visual Studio, for some reason, deletes it when i hit compile or save.
After some digging, i found out that wrapping the content in a namespace UnrealBuidTool.Rules{} solves this. In the end, the build file looks like this:
namespace UnrealBuildTool.Rules
{
public class SpaceShooter : ModuleRules
{
public SpaceShooter(ReadOnlyTargetRules Target) : base(Target)
{
PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;
PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore" });
PrivateDependencyModuleNames.AddRange(new string[] { });
// Uncomment if you are using Slate UI
// PrivateDependencyModuleNames.AddRange(new string[] { "Slate", "SlateCore" });
// Uncomment if you are using online features
// PrivateDependencyModuleNames.Add("OnlineSubsystem");
// To include OnlineSubsystemSteam, add it to the plugins section in your uproject file with the Enabled attribute set to true
}
}
}
As you do not share what is included in your Build.cs file I will just explain what the error is and how you could fix it. The error basically says that it couldn't compile the Build.cs file based on the lines 29 and 25, and that the types that are used there called 'ModuleRules' and 'ReadOnlyTargetRules' could not be found in any of the namespaces that were included by the using statements at the top of Build.cs. These types are both stored in the UnrealBuildTool namespace and can thus be included in your file by typing:
using UnrealBuildTool;
This however seems like a trivial solution to your problem, but as you do not share a lot of info this is the best solution I can give you for now.

The namespace name 'SyntaxTree' could not be found

I have am developing in Unity 2019.2.3f1. I am trying to write a script that can Customize project files created by VSTU. In case the link ever gets removed, I have included a script very similar to the one from the link.
#if ENABLE_VSTU
using System.IO;
using System.Text;
using System.Xml.Linq;
using UnityEditor;
using SyntaxTree.VisualStudio.Unity.Bridge;
[InitializeOnLoad]
public class ProjectFileHook
{
private class Utf8StringWriter : StringWriter
{
public override Encoding Encoding => Encoding.UTF8;
}
static ProjectFileHook()
{
ProjectFilesGenerator.ProjectFileGeneration += (string name, string content) =>
{
// parse the document and make some changes
XDocument document = XDocument.Parse(content);
document.Root?.Add(new XComment("FIX ME"));
// save the changes using the Utf8StringWriter
Utf8StringWriter str = new Utf8StringWriter();
document.Save(str);
return str.ToString();
};
}
}
#endif
The issue is, using SyntaxTree.VisualStudio.Unity.Bridge; fails to compile due to the error error CS0246: The type or namespace name 'SyntaxTree' could not be found (are you missing a using directive or an assembly reference?).
I have checked both Unity and Visual Studio that the Visual Studio Tools for Unity are installed and enabled.
Why is the code failing to compile? Am I missing something that is preventing it from compiling?
Just incase anyone else has this issue.
This issue was being caused by the scripts position. The script has to be under the Editor folder, otherwise the script won't work as expected.
Credits to therealjohn for pointing it out here.

Convert LinqPad C# Program to Visual Studio C# Console Program

This seems obvious but I cannot see an easy way to to do this.
I have written a C# Program in LinqPad and it runs well, is there a guide for moving this to a Visual Studio Console Program.
A simple program such as this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
DataContext dataContext = this;
var products = dataContext.GetTable<Product>();
var query = from p in products
select p;
query.Dump();
}
}
}
produces these errors:
Error 1 The type or namespace name 'DataContext' could not be found (are you missing a using directive or an assembly reference?) c:\users\xxx\documents\visual studio 2013\Projects\ConsoleApplication1\ConsoleApplication1\Program.cs
Error 2 Keyword 'this' is not valid in a static property, static method, or static field initializer c:\users\xxx\documents\visual studio 2013\Projects\ConsoleApplication1\ConsoleApplication1\Program.cs
Error 3 The type or namespace name 'Product' could not be found (are you missing a using directive or an assembly reference?) c:\users\xxx\documents\visual studio 2013\Projects\ConsoleApplication1\ConsoleApplication1\Program.cs 14 49 ConsoleApplication1
I must have to add some references, add a connection to the database, I just need a guide for what to add to my project but I cannot find one.

using namespace in C# from CLI/C++ class

This might be a C#-noob question...
Assume I have the following CLI/C++ header:
namespace DotNet {
namespace V1 {
namespace X {
public ref class AClass {
public:
AClass() {}
void foo() {}
static void bar() {}
};
}
}
}
Then from C# I do the following:
using DotNet;
V1.X.AClass a = new V1.X.AClass();
I get:
Program.cs(18,7): error CS0246: The type or namespace name 'V1' could
not be found (are you missing a using directive or an assembly
reference?)
Same with:
using DotNet.V1;
X.AClass a = new X.AClass();
Program.cs(18,7): error CS0246: The type or namespace name 'X' could
not be found (are you missing a using directive or an assembly
reference?)
What works is:
DotNet.V1.X.AClass a = new DotNet.V1.X.AClass();
Or
using DotNet.V1.X;
AClass a = new AClass();
So either I have to use the full namespace path, or I have to open all of the path to access the class. Is there anything I can do about this?
I would like to be able to open only a part of it.
So either I have to use the full namespace path, or I have to open all of the path to access the class.
Yes.
Is there anything I can do about this?
Not that I'm aware of. Why would you not just want a using directive for the whole namespace? Isn't that the simplest approach?
Note that this has nothing to do with C++/CLI. The same is true whatever the source language is - so you can see it with C# as well:
namespace DotNet.V1.X
{
public class AClass {}
}
As Jon Skeet stated you have to include the full namespace in either the using statement or each time you reference it in code. I recommend just placing the full namespace in the using statement.
You can, however, achieve that syntax with a using alias, but it does not add anything of value outside of syntax.
using V1 = DotNet.V1;
...
V1.X.AClass a = new V1.X.AClass();
Also if you are using C# version 3.0 or higher the var keyword will only require you type out the full namespace on the right side of the assignment.
var a = new DotNet.V1.X.AClass();

QuickGraph, how to use extension method StronglyConnectedComponents

As part of my first experiments with C# (on Mono 2.6.7) , I am trying to use the StronglyConnectedComponents method from QuickGraph. Here is my code:
using System;
using QuickGraph;
using QuickGraph.Data;
using System.Collections.Generic;
using QuickGraph.Algorithms;
namespace Graph
{
class MainClass
{
public static void Main (string[] args)
{
IVertexListGraph<int,Edge<int>> graph;
graph = new AdjacencyGraph<int,Edge<int>>();
IDictionary<int,int> components=new Dictionary<int,int>();
int noc = graph.StronglyConnectedComponents(out components);
}
}
}
When trying to compile the code above, I get the error message (in MonoDevelop):
Error CS1061: Type `QuickGraph.IVertexListGraph<int,QuickGraph.Edge<int>>' does not
contain a definition for `StronglyConnectedComponents' and no extension method
`StronglyConnectedComponents' of type
`QuickGraph.IVertexListGraph<int,QuickGraph.Edge<int>>' could be found
(are you missing a using directive or an assembly reference?) (CS1061) (Graph)
As for as I can see from the documentation, the extension method should be available:
public static int StronglyConnectedComponents<TVertex, TEdge>(
IVertexListGraph<TVertex, TEdge> g,
out IDictionary<TVertex, int> components
)
Also, I referred all three .dll's from QuickGraph. What am I missing?
Ok, I just checked it out and it works for me on Mono 2.10.5 (Ubuntu), which I currently have, so consider updating. 2.6.7 is very old. I just downloaded quick graph library, referenced only one dll (QuickGraph.dll), copypasted your code (just removed using QuickGraph.Data) and it compiles and runs without any problem.

Categories