Answer the question
In order to leave comments, you need to log in
Why do you need partial methods if you can do this (see below)?
An example with partial methods:
partial class Partial_Class_and_Method
{
partial void DoSomethingElseElse();
public void DoSomething()
{
WriteLine("I'm eating!");
DoSomethingElseElse();
}
}
partial class Partial_Class_and_Method
{
partial void DoSomethingElseElse()
{
WriteLine("I don't know what I'm doing!");
}
public void DoSomethingElse()
{
WriteLine("I'm sleeping!");
}
}
partial class Partial_Class_and_Method
{
public void DoSomething()
{
WriteLine("I'm eating!");
DoSomethingElseElse(); //Внимание на это место !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
}
}
partial class Partial_Class_and_Method
{
private void DoSomethingElseElse() //это даже с privat будет работать, ибо КЛАССЫ частичные
{
WriteLine("I don't know what I'm doing!");
}
public void DoSomethingElse()
{
WriteLine("I'm sleeping!");
}
}
Answer the question
In order to leave comments, you need to log in
Hello. There are several situations where it is desirable to separate the definition of a class or method:
When working on large projects, distributing a class across different files allows multiple programmers to work on it at the same time.
When working with an automatically generated source, code can be added to a class without re-creating the source file. Visual Studio uses this approach when generating Windows Forms, Web Service wrapper code, and so on. You can generate code that uses these classes without having to modify the file generated by Visual Studio.
Partial methods allow the developer of one part of a class to define a method similar to an event. The developer of another part of the class can decide whether to implement this method or not. If the method is not implemented, then the compiler removes the method signature and all calls to that method. Method invocations, including any results that might result from the evaluation of the arguments in the invocations, have no run-time effect. Thus, any code in a partial class is free to use the partial method, even if no implementation is provided. During compilation and execution of the program, no errors will occur if the method is called but not implemented.
Partial methods are especially useful for customizing automatically generated code. They allow you to reserve the name and signature of a method so that automatically generated code can call the method and the developer can decide whether to implement the method or not. Like partial classes, partial methods allow automated and human code to work together at no additional cost at run time.
Source: https://docs.microsoft.com/en-us/dotnet/csharp/pro...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question