C#是否可以从一个文件转到另一个文件的给定行?
我想问的是,在C#中,是否可以从一个类转到另一个类的给定行。以下是我的想法的示例:C#是否可以从一个文件转到另一个文件的给定行?,c#,class,oop,goto,C#,Class,Oop,Goto,我想问的是,在C#中,是否可以从一个类转到另一个类的给定行。以下是我的想法的示例: public class Class1 { public void MethodFromClass1() { // Doing some operations before to go in the another class goto: PlaceToGo // Doing some operations after going in the another class
public class Class1
{
public void MethodFromClass1()
{
// Doing some operations before to go in the another class
goto: PlaceToGo
// Doing some operations after going in the another class
}
}
public class Class2
{
public void MethodFromClass2()
{
PlaceToGo:
Console.WriteLine("Print something.");
}
}
使用
goto
运算符不是一种好的做法。你应该避免。在您的情况下,您可以通过另一种方式(创建Class2
的实例并调用其方法):
此外,如果您甚至跳转到此类,对象可能尚未创建,因此没有意义。您可以熟悉goto:
“跳转”到另一个班级对我来说没有太大意义,因为在那之前它从来没有被“建造”过。无论如何,我建议您不要在C#中使用此技术,因为它会使核心难以跟踪和理解。少数合法使用的
goto
可能在switch
语句中:
// Though this is a very stupid example!
int number = 0;
switch (number)
{
case 0:
Console.Write("hello ");
goto case 1;
case 1:
Console.WriteLine("world!");
break;
}
顺便说一句,我从未发现自己在使用goto
,而您的用例只是一个荒谬的例子
也许你想要的是一个事件,这不是别的事情比广播一个呵呵!我要做点什么,有人吗?一些听众会采取行动来回答整个事件。有关详细信息,请参阅以下代码段:
public class Class1
{
private event EventHandler _BeforeDoingStuff;
private event EventHandler BeforeDoingStuff
{
add { _BeforeDoingStuff += value; }
remove { _BeforeDoingStuff -= value; }
}
public void DoStuff()
{
// Do some stuff
// Then fire the event
_BeforeDoingStuff?.Invoke(EventArgs.Empty);
// Continue with more stuff after firing the event
}
}
public class Class2
{
public Class2(Class1 class1)
{
class1.BeforeDoingStuff += (sender, e) =>
{
Console.WriteLine("I did some stuff in the middle of Class1.DoStuff!");
}
}
}
Class1 class1 = new Class1();
Class2 class2 = new Class2(class1);
了解有关C#中事件的更多信息。这应该描述您想要的内容
using System;
namespace ConsoleApp1
{
class FirstMethod
{
public void ExecuteThisNow(int number)
{
Console.WriteLine("This number: " + number + " is going to be skipped over");
}
}
}
namespace ConsoleApp1
{
class SecondMethod
{
private int lengthOfLoop = 10;
private int i = 0;
public void RunTheLoop()
{
for (this.i = 0; i < this.lengthOfLoop; i++)
{
if (this.i == 5)
{
goto ExecuteFirstMethod;
}
else
continue;
}
ExecuteFirstMethod: FirstMethod first = new FirstMethod();
first.ExecuteThisNow(this.i);
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
using System;
class Program
{
static void Main(string[] args)
{
var second = new SecondMethod();
second.RunTheLoop();
Console.ReadLine();
}
}
}
使用系统;
名称空间控制台EAPP1
{
类优先方法
{
public void ExecuteThisNow(整数)
{
WriteLine(“这个数字:“+number+”将被跳过”);
}
}
}
名称空间控制台EAPP1
{
第二类方法
{
私用int lengthOfLoop=10;
私有整数i=0;
public void RunTheLoop()
{
for(this.i=0;i
当然,这样的代码闻起来很难闻,这是非常糟糕的做法。但是,如果您正在玩一个简单的游戏/测试,您可以这样做:
public interface IGoTo
{
void Goto(params object[] p);
}
public class CalleeClass : IGoTo
{
public void Goto(params object[] p)
{
this.MethodFromCallee();
}
public void MethodFromCallee()
{
}
}
public class CallerClass
{
public void MethodFromCaller(IGoTo g)
{
g.Goto();
}
}
static void Main(string[] args)
{
var callee = new CalleeClass();
new CallerClass().MethodFromCaller(callee);
}
如果你需要这样做,那你就大错特错了。C#是一种过程化和面向对象的语言。你应该熟悉这些范例。根据你在代码中的评论,你只需要一个普通的方法调用,而不是典型的去某个地方而不回来的
goto
行为。我不想开始下一个“goto是邪恶的”讨论,但你在这里尝试的是反对一切。只需从class2调用method。为什么转到?也不可能-:“如果当前函数成员中不存在具有给定名称的标签,或者goto
语句不在标签的范围内,则会发生编译时错误。”
public interface IGoTo
{
void Goto(params object[] p);
}
public class CalleeClass : IGoTo
{
public void Goto(params object[] p)
{
this.MethodFromCallee();
}
public void MethodFromCallee()
{
}
}
public class CallerClass
{
public void MethodFromCaller(IGoTo g)
{
g.Goto();
}
}
static void Main(string[] args)
{
var callee = new CalleeClass();
new CallerClass().MethodFromCaller(callee);
}