Can I have two entry points in C# - 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

Related

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

How do you call non static methods from static Main if Main is the starting point

I am new to C# so I hope I am asking this correctly.
I created a new C# project and in it is:
static void Main(string[] args)
{
Consol.Write(myFirstInt());
}
I created a method:
public int myFirstInt()
{
return 5;
}
That is called from the Main. I get that I can't call myFirstInt() because it is not static. However, if Main is the starting point for the program and always has to be static, how do you call non static methods?
You'd have to create a new instance of the class you're running your code in.
Say your code looks like this:
public class YourProgram {
public int myFirstInt(){
return 5;
}
public static void Main(string[] args){
// ...
}
}
You'd have to create a new instance of the YourProgram class like so:
public class YourProgram {
public int myFirstInt(){
return 5;
}
public static void Main(string[] args){
var yourProgram = new YourProgram();
Console.Write(yourProgram.myFirstInt());
}
}
Side note: You made a typo in your code. You wrote Consol.Write which sould be Console.Write. I corrected it in the code above.
Supposing you declared the myFirstInt method in the Program class you just have to do
var program = new Program();
Console.Write(program.myFirstInt());
although i think you just need to change myFirstInt to static

How can be call the method from another class inherited

How can be call the method from another class inherited
let's see my code please:
class Program : classA
{
static void Main(string[] args)
{
// how i can call method ToDo without create an instance like below
//classA c = new classA();
//c.ToDo();
Console.ReadLine();
}
}
class Program2 : classB
{
static void Main(string[] args)
{
// how i can call method ToDo
//ToDo()
Console.ReadLine();
}
}
public abstract class classB
{
public void ToDo()
{
Console.WriteLine("classB");
}
}
public class classA
{
public void ToDo()
{
Console.WriteLine("classA");
}
}
how i can call the method in Either way, please help me.
There are a couple ways to do what you want to do (they're kind of similar or even the same).
One way is to create a class with a static method:
public class classA
{
public static void ToDo()
{
Console.WriteLine("classA");
}
}
then call it like:
classA.ToDo();
Another way is to add another static method to the class that contains Main:
class Program2 : classB
{
static void Main(string[] args)
{
ToDo()
Console.ReadLine();
}
static void Todo()
{
// do stuff here
}
}
If u want to call ToDo() function into [class Program : classA] and [class Program : classB]
Without creating Instance.
U have to Define ToDo() function as static, then u can call this method with class name in anywhere.
public static void ToDo(){}

Async&Await IL Code

If i implement a default async/await method like:
class Program
{
static void Main(string[] args)
{
new Program().TestMethod();
}
private async void TestMethod()
{
await Task.Delay(10);
}
}
The generated IL-Code and the async state machine contains a field "this" with a reference to the instance which invoked the method. If the code is adjusted to a static one, there is of course no "this" value.
Not to do this via reflection, more from perspective of the CLR or the debugger.
Does anyone know how i can determine within the "static TestMethod" from which method i was called - on the IL level!
class Program
{
static void Main(string[] args)
{
TestMethod();
}
private static async void TestMethod()
{
await Task.Delay(10);
}
}
how i can determine within the "static TestMethod" from which method i was called
You can use CallerMemberName attribute
static void Main(string[] args)
{
new Program().TestMethod();
Console.ReadLine();
}
private async void TestMethod([CallerMemberName]string caller = "")
{
Console.WriteLine(caller);
}
PS: You can also use CallerFilePath or CallerLineNumber
using System;
using System.Diagnostics;
namespace Cli
{
class Program
{
static void Main(string[] args)
{
new Program().TestMethod();
}
private void TestMethod()
{
var sf = new StackFrame(1); //get caller stackframe
var mi = sf.GetMethod(); //get method info of caller
Console.WriteLine("{0}::{1}", mi.DeclaringType, mi.Name);
// Cli.Program::Main
}
}
}

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

Categories

Resources