D
D
denismaster2017-11-14 01:07:57
.NET
denismaster, 2017-11-14 01:07:57

How to make a class deconstructor?

Hello everyone) There are several classes:

public class A<TData>
{
  //header
  public string Signature {get;set;}
  public DateTime CreatedDate {get;set;}
  public int Version {get;set;}
  //data
  public TData Data {get;set;}
}

public class B<TData>
{
  //header
  public string Signature {get;set;}
  public DateTime CreatedDate {get;set;}
  public long AuthorId {get;set;}
  public string MagicString {get;set;}
  //data
  public TData Data {get;set;}
}

How to correctly create a class hierarchy and write class deconstructors so that you can write like this:
A<string> first = new A<string>();
B<int> second = new B<int>();
//...
(var headerA, var dataA) = A;
// headerA содержит Signature, CreatedDate, Version
// dataA типа string
(var headerB, var dataB) = B;
// headerB содержит Signature, CreatedDate, AuthorId и MagicString
// dataB типа int
// headerA и headerB поле Data НЕ содержат

Deconstructors are a function void Deconstruct(out SomeClass1 s1, out SomeClass2 s2 ....)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
eRKa, 2017-11-14
@denismaster

First of all, it is not entirely clear which destructor you want. If such as in C ++, then in C # this is not due to the existence of the GC. If you want a method that will be executed before garbage collection, then you need to implement IDisposable. And even if you explicitly call Finalize, then at what point it will be executed, no one will give a guarantee how the GC will decide.
Secondly, C# is a strongly typed language, which means that it needs to somehow understand what type headerA and headerB will be. So for them you need to define the type

class HeaderA
{
      public string Signature {get;set;}
      public DateTime CreatedDate {get;set;}
      public int Version {get;set;}
}

class HeaderB
{
      public string Signature {get;set;}
      public DateTime CreatedDate {get;set;}
      public long AuthorId {get;set;}
      public string MagicString {get;set;}
}

then encapsulate them
public class A<TData>
{
  //header
  public HeaderA Header {get;set;}
  //data
  public TData Data {get;set;}
}

public class B<TData>
{
  //header
  public HederB Header {get;set;}
  //data
  public TData Data {get;set;}
}

It is clear that it is possible to generalize and make
class HederData<THeder, TData>
{
      public THeder Header {get;set;}
      public TData Data {get;set;}
}

Now objects can be created like this
var a = new DataHaeder<HeaderA, string>();
var b = new DataHeader<HeaderB, int>();

Well, actually, we got an object with the fields you want
a.Header and a.Data, b.Header and b.Data;
And by the way, common fields can be put into one class
class CommonProp
{
      public string Signature {get;set;}
      public DateTime CreatedDate {get;set;}
}

And then inherit the headers from it.
class HeaderA : CommonProp
{
      public int Version {get;set;}
}

class HeaderB : CommonProp
{
      public long AuthorId {get;set;}
      public string MagicString {get;set;}
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question