Connecting Tech Pros Worldwide Forums | Help | Site Map

Syntax Question - meaning of tilde (~)

Frankie
Guest
 
Posts: n/a
#1: Sep 14 '07
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 ----------------------



=?Utf-8?B?V0o=?=
Guest
 
Posts: n/a
#2: Sep 14 '07

re: Syntax Question - meaning of tilde (~)


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

WJ

"Frankie" wrote:
Quote:
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 ----------------------
>
>
>
Peter Duniho
Guest
 
Posts: n/a
#3: Sep 14 '07

re: Syntax Question - meaning of tilde (~)


WJ wrote:
Quote:
[...]
Quote:
>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.
Mattias Sjögren
Guest
 
Posts: n/a
#4: Sep 14 '07

re: Syntax Question - meaning of tilde (~)


Quote:
>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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Jarek Kazmierczak
Guest
 
Posts: n/a
#5: Sep 14 '07

re: Syntax Question - meaning of tilde (~)


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();

On Sep 14, 11:16 pm, "Frankie" <A...@B.COMwrote:
Quote:
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 ----------------------

=?ISO-8859-1?Q?Arne_Vajh=F8j?=
Guest
 
Posts: n/a
#6: Sep 15 '07

re: Syntax Question - meaning of tilde (~)


Peter Duniho wrote:
Quote:
WJ wrote:
Quote:
Quote:
>>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.
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
Doug Semler
Guest
 
Posts: n/a
#7: Sep 15 '07

re: Syntax Question - meaning of tilde (~)


"Arne Vajhøj" <arne@vajhoej.dkwrote in message
news:46eb45c9$0$90271$14726298@news.sunsite.dk...
Quote:
Peter Duniho wrote:
Quote:
>WJ wrote:
Quote:
>>>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.
>
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?=
Guest
 
Posts: n/a
#8: Sep 15 '07

re: Syntax Question - meaning of tilde (~)


Doug Semler wrote:
Quote:
"Arne Vajhøj" <arne@vajhoej.dkwrote in message
news:46eb45c9$0$90271$14726298@news.sunsite.dk...
Quote:
>Peter Duniho wrote:
Quote:
>>WJ wrote:
>>>>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.
>>
>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>
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
Doug Semler
Guest
 
Posts: n/a
#9: Sep 15 '07

re: Syntax Question - meaning of tilde (~)


"Arne Vajhøj" <arne@vajhoej.dkwrote in message
news:46eb4bf9$0$90273$14726298@news.sunsite.dk...
Quote:
Doug Semler wrote:
Quote:
>"Arne Vajhøj" <arne@vajhoej.dkwrote in message
>news:46eb45c9$0$90271$14726298@news.sunsite.dk. ..
Quote:
>>Peter Duniho wrote:
>>>WJ wrote:
>>>>>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.
>>>
>>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>
>
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?

Peter Duniho
Guest
 
Posts: n/a
#10: Sep 15 '07

re: Syntax Question - meaning of tilde (~)


Doug Semler wrote:
Quote:
[...]
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
Doug Semler
Guest
 
Posts: n/a
#11: Sep 15 '07

re: Syntax Question - meaning of tilde (~)


"Peter Duniho" <NpOeStPeAdM@NnOwSlPiAnMk.comwrote in message
news:13emnhkp6r4el73@corp.supernews.com...
Quote:
Doug Semler wrote:
Quote:
>[...]
>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?=
Guest
 
Posts: n/a
#12: Oct 2 '07

re: Syntax Question - meaning of tilde (~)


Peter Duniho wrote:
Quote:
Doug Semler wrote:
Quote:
>[...]
>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
Closed Thread


Similar C# / C Sharp bytes