Syntax Question - meaning of tilde (~)

F

Frankie

I have been learning about the AsyncOperation class and came across this
code (sorry I got it a few days ago and don't remember the specific link).

Anyway, I am wondering what the tilde (~) on line 12 does or what it means.
I've been using C# for a while and somehow haven't encountered the tilde
yet.

Thanks.

1. namespace System.ComponentModel
2. {
3. public sealed class AsyncOperation
4. {
5. internal AsyncOperation (SynchronizationContext ctx, object state)
6. {
7. this.ctx = ctx;
8. this.state = state;
9. ctx.OperationStarted ();
10. }
11.
12. ~AsyncOperation ()
13. {
14. if (!done && ctx != null)
15. ctx.OperationCompleted ();
16. }
//remaining code omitted

--------------- END OF OP ----------------------
 
M

Mattias Sjögren

Anyway, I am wondering what the tilde (~) on line 12 does or what it means.
I've been using C# for a while and somehow haven't encountered the tilde
yet.

It's the syntax to define a finalizer method. It compiles to a method
that overrides Object.Finalize(). The syntax is the same as for
destructors in C++.


Mattias
 
P

Peter Duniho

WJ said:
[...]
Anyway, I am wondering what the tilde (~) on line 12 does or what it means.
I've been using C# for a while and somehow haven't encountered the tilde

It means destructor (the same as C++).

No, it doesn't.

It declares the _finalizer_ which is not exactly the same as a C++
destructor.
 
J

Jarek Kazmierczak

It's default destructor of this class,
yes, C# also has something which is called destructors.
You can TRY to use this feature, by typing Object.Finalize();
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Peter said:
No, it doesn't.

It declares the _finalizer_ which is not exactly the same as a C++
destructor.

In the two first edition of the C# standard it was named destructor.

In the third edition they renamed it to finalizer.

To my best knowledge they did not change any semantics.

Arne
 
D

Doug Semler

Arne Vajhøj said:
In the two first edition of the C# standard it was named destructor.

In the third edition they renamed it to finalizer.

To my best knowledge they did not change any semantics.

In C#

To call it "the same" as a C++ destructor is not entirely correct, though.
Finalization can be waaaaay different than C++ destruction. <g>



--
Doug Semler, MCPD
a.a. #705, BAAWA. EAC Guardian of the Horn of the IPU (pbuhh).
The answer is 42; DNRC o-
Gur Hfrarg unf orpbzr fb shyy bs penc gurfr qnlf, abbar rira
erpbtavmrf fvzcyr guvatf yvxr ebg13 nalzber. Fnq, vfa'g vg?
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Doug said:
In C#

To call it "the same" as a C++ destructor is not entirely correct,
though. Finalization can be waaaaay different than C++ destruction. <g>

I am not sure that anyone claimed that a C# destructor/finalizer has the
same semantics as C++.

I at least interpreted "same" as "same syntax".

Arne
 
D

Doug Semler

Arne Vajhøj said:
I am not sure that anyone claimed that a C# destructor/finalizer has the
same semantics as C++.

I at least interpreted "same" as "same syntax".


*You* may have....*BUT* it is important to make sure that the OP understand
that even though it is the same syntax, it is not the same semantics.
Especially if he one were to read this particular message from top to bottom
without context. That's all I'm saying.

Finalizer, destructor....Tomat-oh, Tom-ah-toe. In C#, the semantics what
the function does is completely different than in C++. And that is what is
not "the same"

<g>

--
Doug Semler, MCPD
a.a. #705, BAAWA. EAC Guardian of the Horn of the IPU (pbuhh).
The answer is 42; DNRC o-
Gur Hfrarg unf orpbzr fb shyy bs penc gurfr qnlf, abbar rira
erpbtavmrf fvzcyr guvatf yvxr ebg13 nalzber. Fnq, vfa'g vg?
 
P

Peter Duniho

Doug said:
[...]
Finalizer, destructor....Tomat-oh, Tom-ah-toe. In C#, the semantics
what the function does is completely different than in C++. And that is
what is not "the same"

And the post to which I replied read "It means destructor (the same as
C++)". The word "means" implies "semantics", whether that was intended
or not. That's what semantics is...the _meaning_.

I don't see how any reasonable interpretation infers "syntax" from the
word "means". I talk about "syntax" when I'm discussion how things are
written, and "semantics" when I'm discussion what those things mean.

But regardless, it's very important to understand that a finalizer isn't
a destructor. It's such an important difference, they even changed the
name to make that more clear.

Pete
 
D

Doug Semler

Peter Duniho said:
Doug said:
[...]
Finalizer, destructor....Tomat-oh, Tom-ah-toe. In C#, the semantics
what the function does is completely different than in C++. And that is
what is not "the same"

And the post to which I replied read "It means destructor (the same as
C++)". The word "means" implies "semantics", whether that was intended or
not. That's what semantics is...the _meaning_.

I don't see how any reasonable interpretation infers "syntax" from the
word "means". I talk about "syntax" when I'm discussion how things are
written, and "semantics" when I'm discussion what those things mean.

But regardless, it's very important to understand that a finalizer isn't a
destructor. It's such an important difference, they even changed the name
to make that more clear.

Pete


Pete,

I was talking syntax in terms of the original statement. This is the
problem with the English language. Here's two completely different
interpretations of "It [the ~] means destructor (the same as C++)":

1) As in the C++ language, the ~ character in front of the name makes that
the function is a destructor.
2) The ~ character in front of the function name makes the function act as a
destructor as in the a c++ language.

See how the syntax changes the semantics? In statement 1, the modification
is on the ~ character. In statement 2, the modification is on the word
"destructor". Which one is correct in terms of C#? Semantics and syntax
cannot be divorced from each other. (Which is probably why I like
programming languages better than natural languages)

--
Doug Semler, MCPD
a.a. #705, BAAWA. EAC Guardian of the Horn of the IPU (pbuhh).
The answer is 42; DNRC o-
Gur Hfrarg unf orpbzr fb shyy bs penc gurfr qnlf, abbar rira
erpbtavmrf fvzcyr guvatf yvxr ebg13 nalzber. Fnq, vfa'g vg?
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Peter said:
Doug said:
[...]
Finalizer, destructor....Tomat-oh, Tom-ah-toe. In C#, the semantics
what the function does is completely different than in C++. And that
is what is not "the same"

And the post to which I replied read "It means destructor (the same as
C++)". The word "means" implies "semantics", whether that was intended
or not. That's what semantics is...the _meaning_.

I don't see how any reasonable interpretation infers "syntax" from the
word "means". I talk about "syntax" when I'm discussion how things are
written, and "semantics" when I'm discussion what those things mean.

Tilde is a syntactical element.

If someone ask what ^ means will you tell him that it means XOR or
will you tell him what XOR actually does ?

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top