Define C# class outside of any namespace - c#

Is it possible to define a class (not interface) outside of any namespace?

Ran a test and yes you can. Here's my code built off of a Console App:
using System;
using System.Text;
namespace With_Console_App
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("This will output something: ");
Console.ReadLine();
some.Print();
Console.ReadLine();
}
}
}
class some
{
public static void Print()
{
Console.WriteLine("something");
}
}
Yes, you can define a class outside of a namespace. Per juharr it ends up in the default global namespace.

Related

How do I use a function inside a seperate C# file?

So I am new to C# and done some researching on how to do this but I still don't get it.
I have 2 files, file foo and file bar. File foo is my main file and I want to use a function from inside bar inside foo. The function's purpose is not important because I am just playing around for now. As of now it is an alternative method to print text into the console. I am getting this error message when I try to execute the command csc foo.cs:
foo.cs(9,13): error CS0103: The name 'message' does not exist in the current context
foo.cs
using System;
namespace main
{
class program
{
static void Main(string[] args)
{
message.print("Hello World!"); //line 9
Console.ReadLine();
}
}
}
bar.cs
using System;
namespace main
{
public class message
{
public void print(string Message)
{
Console.WriteLine(Message);
}
}
}
any help would be much appreciated
ALSO: note that both files are in the same directory and both classes are in the same namespace.
Because screenshot was
requested
using System;
namespace main
{
class program
{
static void Main(string[] args)
{
message m=new message();
m.print("Hello World!"); //line 9
Console.ReadLine();
}
}
}
You should create class instance, and then call the method of that variable.
Not that class names should start with capital first letter.
Try this:
using System;
public class Program
{
public static void Main()
{
Message msg = new Message();
msg.print("Hello World!"); //line 9
Console.ReadLine();
}
public class Message
{
public void print(string Message)
{
Console.WriteLine(Message);
}
}
}
Here is a working solution. snippet
Alternatively to the other answers, print could be static. Also note that the C# convention is that file, namespace, class and method names are in upper camel case, while parameters are in lower camel case. Another convention is to name the file the same as the class.
Program.cs
using System;
namespace Main
{
class Program
{
static void Main(string[] args)
{
Message.Print("Hello World!");
Console.ReadLine();
}
}
}
Message.cs
using System;
namespace Main
{
public class Message
{
public static void Print(string message)
{
Console.WriteLine(message);
}
}
}
Edit:
Earlier I wrote that Main must be public, because .NET Fiddle required it. However, this is apparently not generally the case.
OK. I found an answer. There were many problems but at the end I had to change up the command I was using.
At first I was using csc foo.cs
then I changed it to csc foo.cs bar.cs
As of yet I am unsure of exactly why but I will provide updates when I figure it out.
https://www.c-sharpcorner.com/UploadFile/puranindia/CommandLineCompilerOptions11082009234932PM/CommandLineCompilerOptions.aspx
There is a documentation for options for the csc command.

How to fix "} expected" when trying to create an enum in a console application? [duplicate]

This question already has answers here:
Define enums within a method in C#?
(5 answers)
Closed 3 years ago.
I'm trying to create an enum in c# using a visual studio console application, yet the opening curly bracket of my Main method gives an error saying that I need a closing curly bracket.
I've tried copying and pasting the code for the enum into a windows forms application, and I don't get an error there.
using System;
namespace autoDagwaarde
{
class Program
{
static void Main(string[] args)
{
enum BrandstofSoort { bezine, diesel };
}
}
}
I expect this to work, since I use the right amount of opening and closing curly brackets, but instead I get an error right below "static void Main", "} expected".
enum should be defined inside a namespace, not inside a method.
Define your enum outside the main function:
using System;
namespace autoDagwaarde
{
class Program
{
enum BrandstofSoort { bezine, diesel };
static void Main(string[] args)
{
}
}
}
Also note it's called an enumeration and not an enumerator which is something else in C#!
A enum is a class itself, so to define it you need to do is outside the Main.
Would be here:
namespace ConsoleApp1
{
class Program
{
enum BrandstofSoort
{
bezine,
diesel
}
static void Main(string[] args)
{
}
}
}
Or here:
namespace ConsoleApp1
{
enum BrandstofSoort
{
bezine,
diesel
}
class Program
{
static void Main(string[] args)
{
}
}
}
It can be also the namespace so the class would be in the globalnamespace.
Then you can use an instance of your defined class in the main.
Hope that helps
using System;
namespace autoDagwaarde
{
class Program
{
enum BrandstofSoort { bezine, diesel };
static void Main(string[] args)
{
// write your logic here
}
}
}

how to add multiple names spaces and Classes within those for one solution

As a first simple exercise, I ask you to create a new C# Console App in Visual Studio.
This app must have 2 different namespaces, and each namespace contain at least one class with a method to display the word "Hello, this is XXX class" in console, where XXX is the parent class. You can code it on the same file or create different files for different namespaces.
In the Main method of the default class called Program, you must be able to show all the different "Hello..." messages in console, being able to call different classes and methods you just have created.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace First
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello this is The FIRST Class.");
Console.ReadLine();
}
}
}
namespace Second
{
internal class Program
{
static void Main1(string[] args)
{
Console.WriteLine("Hello, this is the SECOND Class.");
Console.ReadLine();
}
}
}
I was hoping both Name spaces would show the individual classes. Not sure what I'm doing wrong here
You should be creating two classes and should call respective methods from Main method.
using First;
using Second;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication16
{
class Program
{
static void Main(string[] args)
{
FirstClass fc = new FirstClass();
fc.DisplayHello();
SecondClass sc = new SecondClass();
sc.DisplayHello();
}
}
}
namespace First
{
internal class FirstClass
{
public void DisplayHello()
{
Console.WriteLine("Hello this is The FIRST Class.");
}
}
}
namespace Second
{
internal class SecondClass
{
public void DisplayHello()
{
Console.WriteLine("Hello this is The SECOND Class.");
}
}
}

Expected class, delegate, enum, interface, or struct (CS1518)

simple code here and the answers I find don't seem to work.
I'm using
SharpDevelop Version : 3.2.1.6466
.NET Version : 2.0.50727.5485
The problem is the error code
Expected class, delegate, enum, interface, or struct (CS1518).
Any ideas?
Program.cs codes:
using System;
using System.Threading;
namespace Threshold
{
public class Class1
{
public Class1()
{
Heritage YOLO = new Heritage();
YOLO.Fractal();
}
}
static void Main()
{
//do nothing
}
}
The cs file it calls is:
using System;
using System.Threading;
namespace Threshold
{
public class Heritage
{
int Fractal()
{
//Do stuff.
}
}
internal partial class DefineConstants
{
public const string DRIVERPATH = "d:\\tc\\bgi";
}
}
Please help with a fix.
Thanks.
Your main method is outside the class. Put it inside.

How to call a method from a different class

At the moment I have created a new method in a new class, and I am trying to call this method from my main class:
Program.cs:
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
testing();
Console.ReadLine();
}
}
}
and the method is in "Class1.cs":
namespace ConsoleApplication2
{
class Class1
{
public static void testing()
{
System.Console.WriteLine("It works!");
}
}
}
You need to specify the name of the class that the method is on. So:
Class1.testing();
Sometimes you might of course need to also worry about the namespace that Class1 is in. In this case both Class1 and Main are in the same namespace. If they hadn't been though then you'd have had to call it like:
ConsoleApplication2.DifferentNamespace.Class1.testing();
or with a using declaration at the top of program.cs:
using ConsoleApplication2.DifferentNamespace
You have made testing a static method, so you can call the method in this fashion
static void Main(string[] args)
{
Class1.testing();
Console.ReadLine();
}
Is this what you want ?
You are missing the class declaration in order to use the static method:
Class1.testing();
Class 1 has to be a public class and then you can call Class1.testing()

Categories