Console closing instantly not going through the code - c#

Just been learning C# methods on SOLO LEARN, i'm on method parameters the app have it's own compiler and totally different from visual studio so i can't get this code to work. When i try to run the console it closes instantly even tho i have Console.Read();
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
}
void myFunc(int x)
{
int result = x / 2;
Console.WriteLine(result);
Console.Read();
}
}
}

You don't call anything from Main(). Call myFunc() from Main():
static void Main(string[] args)
{
myFunc(4);
}
Also you should make myFunc() to be static:
static void myFunc(int x)
OR
You can create instance of Program and call this function without making myFunc() static (simply change your Main() method):
static void Main(string[] args)
{
Program p = new Program();
p.myFunc(4);
}

You are not executing the function you have created inside your main.
In a console app static void Main(string[] args) is the function where application starts.
So, in order to have your application started you need to execute your myFunc(int x) function inside it.
Make the changes like below:
static void Main(string[] args)
{
myFunc(4); //You need to put an integer here
}

Related

Can I have two entry points in C#

Would it be possible to use two entry points in C# instead of just having the one. For example when I have this:
using System;
namespace learning
{
class cool
{
static void Main(string[] args)
{
}
}
}
Would it be possible to have another entry point such as secondary that the program executes once the main entry point has finished.
You may want to do something like this:
class Program {
public static void EntryPoint1(string[] args) {
// Code
}
public static void EntryPoint2(string[] args) {
// Code
}
public static void Main(string[] args) {
EntryPoint1(args);
EntryPoint2(args);
}
}
Just make sure to not modify args during EnteryPoint1 or if you want to, clone them like this:
class Program {
public static void EntryPoint1(string[] args) {
// Code
}
public static void EntryPoint2(string[] args) {
// Code
}
public static void Main(string[] args) {
EntryPoint1(args.Clone());
EntryPoint2(args.Clone());
}
}
In C#, you specify the entry point using the /main: compiler option.
Imagine that the code containing containing two main() methods as follow :
namespace Application {
class ClassA {
static void main () {
// Code here
}
}
class ClassB {
static void main () {
// Code here
}
}
To use ClassA.main() as your entry point, you would specify the following when compiling:
csc /main:Application.ClassA hello.cs
You can only have a single entry point, but you can write two separate methods, call the first one, and then the second one. You will achieve what you're describing.
using System;
namespace learning
{
class cool
{
static void Main(string[] args)
{
PartOne();
PartTwo();
}
void PartOne() {
// Something happens here
}
void PartTwo() {
// Something else happens here
}
}
}
Additionally (depending on how the program starts up) you can send in arguments to specify which method you want to execute (if you don't need both of them to execute). Then you can just do an "if/else" statement that will decide which method to run depending on the arguments passed into Main

.NET 6: How to use method overloading in console app Startup?

.NET 6 offers boilerplate-removal in console applications Startup class. I try to run this simple test code:
Console.WriteLine("Hello, World!");
static void Test(int a, int b) { }
static void Test(int a, int b, int c) { }
I'm getting compiler error in the last line:
Error CS0128 A local variable or function named 'Test' is already defined in this scope
My question is: How to use method overloading in boilerplate free Startup pattern?
This
Console.WriteLine("Hello, World!");
static void Test(int a, int b) { }
is compiled to
[CompilerGenerated]
internal class Program
{
private static void <Main>$(string[] args)
{
Console.WriteLine("Hello, World!");
static void Test(int a, int b)
{
}
}
}
You see that the method is a local method, ie declaring inside Main. You cannot have 2 local functions with the same name. THis also fails;
see https://github.com/dotnet/docs/issues/17275
namespace Foo {
class Program {
// SerialPort port;
static void Main(string[] args) {
static void Test(int a) { };
static void Test(int a, int b) { };
}
}
for the same reason. The new short Main syntax has an awful lot of limitations
You can still create a class and put all the methods you want to overload. That's what I did. It works fine even if you can add access modifiers because as #pm100 said, the methods after compilation are in the main method
[CompilerGenerated]
internal class Program
{
private static void <Main>$(string[] args)
{
Console.WriteLine("Hello, World!");
static void Test(int a, int b)
{
}
}
}
so it's the same for access modifiers but hopefully the next version of .NET won't have this problem anymore but for now you can always use a previous version that keeps the "Main" method.

How to call a class rather than a method inside a class?

so I want to make it easy to call Console.WriteLine() (from the namespace system) so I wrote:
using System;
namespace main
{
namespace easier
{
public class print
{
public print(string input)
{
Console.WriteLine(input);
}
}
}
}
But when I try to call print("Hello World); it says CS1955: Non-invocable member 'print' cannot be used like a method.
Is there a way so I don't have to do class.print("Hello World"); just print("Hello World");?
Thanks in advance!
You can use using static
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-static
using System;
using static Easier;
class Program
{
static void Main(string[] args)
{
Print("message");
}
}
public static class Easier
{
public static void Print(string value)
{
Console.WriteLine(value);
}
}

Call Methode in process from c#-project

I want to call a public Methode(Send) in a process from my c# project!
This is the process with the Methode that i want to call :
namespace Test123
{
class Program
{
static void Main(string[] args)
{
while(true)
{
}
}
public void Send()
{
Console.WriteLine("Test");
}
}
}
I know how to get the process but not how to invoke the methode!
I already searched on other websites and i dinĀ“t found anything that helps me.
Create a instance of the program in the static and call the method.
class Program
{
static void Main(string[] args)
{
var p = new Program();
while(true)
{
p.Send();
}
}
public void Send()
{
Console.WriteLine("Test");
}
}
If your in the same Program class, you can just call the method. The other approach listed where you declare a new Program, would be needed if you were in a different class.
static void Main(string[] args)
{
while(true)
{
Send();
}
}

Task.Factory.StartNew() not working for me

I wrote this small application. for some reason i am not being able to print "Hello from a thread" when i run this program. however if i debug it and put a breakpoint inside Do() method, it does print.
Any ideas?
using System;
using System.Threading.Tasks;
namespace ConsoleApplication3
{
internal class Program
{
private static void Main(string[] args)
{
Task.Factory.StartNew(Do);
}
private static void Do()
{
Console.WriteLine("Hello from a thread");
}
}
}
Are you sure that the program simply isn't closing before you can see the output? Because this works fine for me.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
private static void Main(string[] args)
{
Task.Factory.StartNew(Do);
Console.Read();
}
private static void Do()
{
Console.WriteLine("Hello from a thread");
}
}
}
Edit: Added the comment I wrote in response to this, including my reasoning to why the text wasn't printed.
Its either because the application closes down before the thread has the possibility of outputting the string to the screen. It's also possible that you simply cant see it because it closes straight away. Either way the reason it worked with the breakpoint is because you keep the application alive longer.
Try this.
using System;
using System.Threading.Tasks;
namespace ConsoleApplication3
{
internal class Program
{
static void Main(string[] args)
{
Task.Factory.StartNew(Do);
Console.ReadKey();
}
static void Do()
{
Console.WriteLine("Hello from a thread");
}
}
}

Categories

Resources