在委托的上下文中,术语回调是否意味着" 代理委托它为另一个代表工作以完成某项任务 "?
示例:( 根据我的理解,我已经实现了回调,如果错误则纠正我)
namespace Test
{
public delegate string CallbackDemo(string str);
class Program
{
static void Main(string[] args)
{
CallbackDemo handler = new CallbackDemo(StrAnother);
string substr = Strfunc(handler);
Console.WriteLine(substr);
Console.ReadKey(true);
}
static string Strfunc(CallbackDemo callback)
{
return callback("Hello World");
}
static string StrAnother(string str)
{
return str.Substring(1, 3).ToString();
}
}
}
Run Code Online (Sandbox Code Playgroud)
请根据需要提供示例.
请考虑以下代码(为简单起见,我没有遵循任何C#编码规则).
public class Professor
{
public string _Name;
public Professor(){}
public Professor(string name)
{
_Name=name;
}
public void Display()
{
Console.WriteLine("Name={0}",_Name);
}
}
public class Example
{
static int Main(string[] args)
{
Professor david = new Professor("David");
Console.WriteLine("\nBefore calling the method ProfessorDetails().. ");
david.Display();
ProfessorDetails(david);
Console.WriteLine("\nAfter calling the method ProfessorDetails()..");
david. Display();
}
static void ProfessorDetails(Professor p)
{
//change in the name here is reflected
p._Name="Flower";
//Why Caller unable to see this assignment
p=new Professor("Jon");
}
}
Run Code Online (Sandbox Code Playgroud)
正如预期的那样,输出是:
在调用方法之前,教授Details()......
姓名=大卫 …
我相信(如果我错了,请纠正我),根据值类型的C#规则,没有默认构造函数.CLR将定义用于将字段值清零的那个.
供参考类型:
class Test
{
private string Name;
}
Run Code Online (Sandbox Code Playgroud)
默认构造函数是由C#还是CLR提供的?
我对理解静态引用有点困惑.
当我声明时,我们可以理解实例引用
Car myCar = new Car();
Car yourCar = new Car();
--------------- --------------
stack Managed Heap
---------------- --------------
-----
myCar ------- > points to Car
-----
YourCar ------- > points to Car
-----
----------------- ---------------
Run Code Online (Sandbox Code Playgroud)
如果是静态类怎么办?当我宣布时,我可以这么说吗?
staticClass.MyMethod( )
-----------------
Managed Heap
----------------
staticClass
(Memory Address )
-----------------
Run Code Online (Sandbox Code Playgroud)
更新:因为类是蓝图而对象是物理实体;当我声明staticClass.MyMethod或staticClass.MyField = value时,是否包含静态类,我是否直接与堆交互?(因为静态类不允许实例).
根据定义:
"由于接口本身不是一个对象,我无法初始化它.如果允许接口声明文件,那么它需要存储位置,所以我们不能在接口内声明字段."
有财产说的例子
当我宣布
string SayHello { get; set; }
Run Code Online (Sandbox Code Playgroud)
在界面内
它get_SayHello( ) ,set_SayHello()在IL 内部挂钩 (当我反汇编时,我可以看到get和set方法).
我的问题仍然是属性需要一些存储位置,那么属性声明如何
允许在界面内.
编辑:这就是我的理解.由于我是C#的新手,我正在寻求你的帮助.
我不确定以下问题是否有效.为了教育自己,我只是在问.
(1)我可以以编程方式迭代所有代的GC堆吗?
(2)是否可以通过启动线程来观察GC如何对我的装配进行操作?
任何可用于C#3.0或3.5中的Collection和Generics的备忘单?
根据MSDN文档接口可以是类或命名空间的成员:
例如,我可以声明:
public class Test
{
public interface IMemberofTest
{
void get();
}
}
Run Code Online (Sandbox Code Playgroud)
在类中使用接口有什么用?它不会打破真正的界面使用的目的吗?
通常(根据我的理解)我必须遵循很多步骤
覆盖"等于"以检查对象的状态.
示例:
public override bool Equals(object obj)
{
if (obj is SalesPerson && obj != null)
{
SalesPerson temp;
temp = (SalesPerson)obj;
if (temp.fName == this.fName && temp.lName == this.fName
&& temp.personAge == this.personAge )
{
return true;
}
else
{
return false;
}
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
LINQ或其他技术的任何其他选择给我快捷代码?
更新:
此外,当我覆盖"等于"时,我也必须覆盖GetHasCode().
由于lock是System.Threading.Monitor的间接表示,如果我希望直接使用Monitor,我可以获得任何额外的好处.(我读过一篇文章,它建议总是使用Monitor来获得额外的好处.但是没有这些好处的解释)