内部在C#与VB中的朋友 [英] Internal in C# vs. Friend in VB

查看:65
本文介绍了内部在C#与VB中的朋友的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一些代码从VB转换为C#。我在VB中将朋友功能作为C#中的内部功能。但是,与内部函数不同,可以在没有对象的情况下调用friend函数。所以,我将内部函数设置为静态,以便我可以使用类名称来调用它(类似于friend函数的工作方式)。我对此并不满意。有一个更好的方法吗 ? A类和B类当然在同一个组件中。



我尝试过的方法:



  A {
内部 void function(){ // 做某事}
}
class B {
void CallFunction() {A.function(); // 不可能!
}
}

解决方案

查看此图表。它将为您解答此问题以及许多其他问题... VB.NET和C#比较 [ ^ ] ...它帮助我过渡到C#。

VB的朋友相当于C#的内部 - 它不允许您访问没有实例的方法只是使用任何一种语言的类名。要在VB中执行此操作,请指定共享 - 对于C#,它是 static



我怀疑你的VB方法标记为朋友共享而不仅仅是朋友 - 其中允许他们实例免费访问,就像C#内部静态方法一样。


I'm trying to convert some code from VB to C#. I made a "friend" function in VB as "internal" in C#. However, friend functions can be called without an object unlike internal functions. So, I made the internal function static so that I could call it using the class name (similar to how the friend function worked). I'm not very happy with this. Is there a better way to do this ? Classes A and B are in the same assembly of course.

What I have tried:

class A{
internal void function(){//do something}
}
class B{
void CallFunction(){A.function(); //not possible !
}
}

解决方案

Have a look at this chart. It will answer this and many other questions for you... VB.NET and C# Comparison[^] ... it helped me with my transition to C#.


VB's Friend is the equivalent of C#'s internal - it does not allow you to access the method without an instance just by using the class name in either language. To do that in VB, you specify Shared - for C# it's static.

I suspect that your VB methods are marked as Friend Shared rather than just Friend - which would allow them instance-free access, just like a C# internal static method.


这篇关于内部在C#与VB中的朋友的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆