D
D
Denis Bredun2021-07-06 22:10:26
C++ / C#
Denis Bredun, 2021-07-06 22:10:26

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

Example without partial methods:
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!");
    }
  }

I look (looked?) at the functionality of partial methods as that, regardless of where the body of the method implementation is located, we can use the method in both the first and second classes, which is convenient for team work, when, for example, we need in class B, use the method of class A (I know that the name should be the same, I just named it that way for convenience), for which, as I thought before, you need not only to make partial classes, but also methods, but, like, just enough that I will make the classes partial.
Then please tell me and explain what exactly partial methods are for. I heard about auto-generated code and what they can be used there, but I didn’t understand what kind of “auto-generated code” it was, maybe I just haven’t gotten around to working with it yet; if you feel comfortable, then tell us about it, as well as about other cases when partial methods may be needed.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Baryshev, 2021-07-06
@Luffy1

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 question

Ask a Question

731 491 924 answers to any question