Answer the question
In order to leave comments, you need to log in
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;}
}
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 НЕ содержат
Answer the question
In order to leave comments, you need to log in
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;}
}
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;}
}
class HederData<THeder, TData>
{
public THeder Header {get;set;}
public TData Data {get;set;}
}
var a = new DataHaeder<HeaderA, string>();
var b = new DataHeader<HeaderB, int>();
class CommonProp
{
public string Signature {get;set;}
public DateTime CreatedDate {get;set;}
}
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 questionAsk a Question
731 491 924 answers to any question