[Mono-list] Question about C#

Ricardo Kirkner mono@kirkner.com.ar
Tue, 10 Sep 2002 00:49:08 -0300


This is a multi-part message in MIME format.

------=_NextPart_000_000C_01C25863.DF2D74E0
Content-Type: text/plain;
	charset="utf-8"
Content-Transfer-Encoding: quoted-printable



  On Mon, 2002-09-09 at 22:15, Ricardo Kirkner wrote:=20
    I am sorry to bother the list with this question, but I could not =
find a satisfying answer anywhere.=20
     =20
    My question regards overloading operators. As far as I know, if you =
want to overload some operator, it must be public and static.=20
     =20
    1. I dont understand why this have to be that way=20
  I would guess that the `static' requirement is for language =
simplification -- there is only one (consistent) way for operators to be =
expressed.  In C++, some operators must be global (read: operator<<, =
operator>>).  C# doesn't have global scope, so class-static is the =
closest equivalent.  Since some operators would need to be class-static, =
it was probably more consistent to just require that they all be =
class-static.  That's my best guess, anyway.=20

  I have no idea why  `public' is required.=20
    2. If I want to overload the ++ operator in a class that I dont want =
to be public, but internal or private, how can I prevent someone from =
accessing a method (the ++ operator) that has been defined public?=20
  Question to ponder: how will they access the public method if they =
can't access the class in the first place?  Consider methods that must =
be public, such as Object.ToString().  Is it a problem that this method =
is public, even in your internal classes?  Not usually -- non-internal =
users can't access the class, so the existence of a public ToString() =
method shouldn't be a problem.  The same should be true of any =
operators.=20

  (The answer to the above question of accessing internal classes is =
simple, actually: use Reflection and poke around...  Alternatively, =
modify the runtime to permit poking on internal data.  It's hard to =
protect against the runtime....  However, these ideas are not trivial, =
and can probably be ignored most of the time.)=20
This is not really a clean solution, is it? I mean, although I cannot =
access the class (so I dont have to worry about the "public-nes" of the =
methods inside that class, it does not mean that it is correct to force =
you to declare the method public. (although this could be, and probably =
is, just a simplification made by the language designers)
  However, the real question remains: why do you need an assignment =
operator?  Assignment operators are useful in C++ when wrapping =
resources, such as memory, files, thread locks, etc., to make sure that =
the resource is properly managed (in concert with the copy constructor =
and destructor)...  In C#, the garbage collector is used for resource =
management, removing (what I've found) the greatest need for the trio of =
C++ copy constructor, assignment operator, and destructor.  If you need =
something more deterministic, the IDisposable interface/idiom is =
appropriate.=20

I actually dont have a need for an assignment operator, but I think it =
is really nice if you can do something like

    obj1 =3D obj2;

instead of
   =20
    obj1 =3D new ObjectType(obj2);

This whole issue arises because c# treats objects as references to =
objects instead as the real ones (not that I find this wrong). I think =
that if you use a language that tries to be so simple as c# is, and that =
tries to "help" the programmer by simplifying syntax, etc, then it is =
suspicious not being able to write a simple object assignment  (without =
having both variables referencing the same object. Note that by simle I =
mean using the =3D operator). This could be just a decision made by the =
folks who thought about the language (and then I could not make anything =
about it, of course), so I dont want to make a big discussion about this =
here (because it is really a C# topic and not mono specific).

Finally, I want to thank you Jonathan, for your comments, I believe they =
helped

With regards,

Ricardo Kirkner

------=_NextPart_000_000C_01C25863.DF2D74E0
Content-Type: text/html;
	charset="utf-8"
Content-Transfer-Encoding: quoted-printable

=EF=BB=BF<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; CHARSET=3DUTF-8">
<META content=3D"MSHTML 6.00.2719.2200" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<BLOCKQUOTE=20
style=3D"PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; =
BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
  <DIV><FONT face=3DArial size=3D2></FONT><FONT face=3DArial=20
  size=3D2></FONT><BR></DIV>On Mon, 2002-09-09 at 22:15, Ricardo Kirkner =
wrote:=20
  <BLOCKQUOTE><FONT color=3D#737373><FONT size=3D2><I>I am sorry to =
bother the=20
    list with this question, but I could not find a satisfying answer=20
    anywhere.</FONT></FONT></I> <BR><FONT color=3D#737373><FONT=20
    size=3D3><I>&nbsp;</FONT></FONT></I> <BR><FONT color=3D#737373><FONT =

    size=3D2><I>My question regards overloading operators. As far as I =
know, if=20
    you want to overload some operator, it must be public and=20
    static.</FONT></FONT></I> <BR><FONT color=3D#737373><FONT=20
    size=3D3><I>&nbsp;</FONT></FONT></I> <BR><FONT color=3D#737373><FONT =

    size=3D2><I>1. I dont understand why this have to be that=20
    way</FONT></FONT></I> </BLOCKQUOTE><FONT size=3D2>I would guess that =
the=20
  `static' requirement is for language simplification -- there is only =
one=20
  (consistent) way for operators to be expressed.&nbsp; In C++, some =
operators=20
  must be global (read: operator&lt;&lt;, operator&gt;&gt;).&nbsp; C# =
doesn't=20
  have global scope, so class-static is the closest equivalent.&nbsp; =
Since some=20
  operators would need to be class-static, it was probably more =
consistent to=20
  just require that they all be class-static.&nbsp; That's my best =
guess,=20
  anyway.</FONT> <BR><FONT size=3D2></FONT><BR><FONT size=3D2>I have no =
idea=20
  why&nbsp; `public' is required.</FONT>=20
  <BLOCKQUOTE><FONT color=3D#737373><FONT size=3D2><I>2. If I want to =
overload the=20
    ++ operator in a class that I dont want to be public, but internal =
or=20
    private, how can I prevent someone from accessing a method (the ++ =
operator)=20
    that has been defined public?</FONT></FONT></I><FONT =
color=3D#737373><FONT=20
    size=3D3><I> </FONT></FONT></I></BLOCKQUOTE>
  <DIV><FONT size=3D3>Question to ponder: how will they access the =
public method=20
  if they can't access the class in the first place?&nbsp; Consider =
methods that=20
  must be public, such as Object.ToString().&nbsp; Is it a problem that =
this=20
  method is public, even in your internal classes?&nbsp; Not usually --=20
  non-internal users can't access the class, so the existence of a =
public=20
  ToString() method shouldn't be a problem.&nbsp; The same should be =
true of any=20
  operators.</FONT> <BR><FONT size=3D3></FONT><BR><FONT size=3D3>(The =
answer to the=20
  above question of accessing internal classes is simple, actually: use=20
  Reflection and poke around...&nbsp; Alternatively, modify the runtime =
to=20
  permit poking on internal data.&nbsp; It's hard to protect against the =

  runtime....&nbsp; However</FONT><FONT size=3D3>, these ideas are not =
trivial,=20
  and can probably be ignored most of the time.)</FONT> =
</DIV></BLOCKQUOTE>
<DIV><FONT face=3DArial size=3D2>This is not really a <EM>clean =
</EM>solution, is=20
it? I mean, although I cannot access the class (so I dont have to worry =
about=20
the "public-nes" of the methods inside that class, it does not mean that =
it is=20
correct to force you to declare the method public. (although this could =
be, and=20
probably is, just a simplification made by the language =
designers)</FONT></DIV>
<BLOCKQUOTE=20
style=3D"PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; =
BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
  <DIV><FONT size=3D3>However, the real question remains: why do you =
need an=20
  assignment operator?&nbsp; Assignment operators are useful in C++ when =

  wrapping resources, such as memory, files, thread locks, etc., to make =
sure=20
  that the resource is properly managed (in concert with the copy =
constructor=20
  and destructor)...&nbsp; In C#, the garbage collector is used for =
resource=20
  management, removing (what I've found) the greatest need for the trio =
of C++=20
  copy constructor, assignment operator, and destructor.&nbsp; If you =
need=20
  something more deterministic, the IDisposable interface/idiom is=20
  appropriate.</FONT> <BR></DIV></BLOCKQUOTE>
<DIV dir=3Dltr style=3D"MARGIN-RIGHT: 0px"><FONT face=3DArial size=3D2>I =
actually dont=20
have a <EM>need</EM> for an assignment operator, but I think it is =
really nice=20
if you can do something like</FONT></DIV>
<DIV dir=3Dltr style=3D"MARGIN-RIGHT: 0px"><FONT face=3DArial=20
size=3D2></FONT>&nbsp;</DIV>
<DIV dir=3Dltr style=3D"MARGIN-RIGHT: 0px"><FONT face=3DArial=20
size=3D2>&nbsp;&nbsp;&nbsp; obj1 =3D obj2;</FONT></DIV>
<DIV dir=3Dltr style=3D"MARGIN-RIGHT: 0px"><FONT face=3DArial=20
size=3D2></FONT>&nbsp;</DIV>
<DIV dir=3Dltr style=3D"MARGIN-RIGHT: 0px"><FONT face=3DArial =
size=3D2>instead=20
of</FONT></DIV>
<DIV dir=3Dltr style=3D"MARGIN-RIGHT: 0px"><FONT face=3DArial=20
size=3D2>&nbsp;&nbsp;&nbsp; </FONT></DIV>
<DIV dir=3Dltr style=3D"MARGIN-RIGHT: 0px"><FONT face=3DArial=20
size=3D2>&nbsp;&nbsp;&nbsp; obj1 =3D new ObjectType(obj2);</FONT></DIV>
<DIV dir=3Dltr style=3D"MARGIN-RIGHT: 0px"><FONT face=3DArial=20
size=3D2></FONT>&nbsp;</DIV>
<DIV dir=3Dltr style=3D"MARGIN-RIGHT: 0px"><FONT face=3DArial =
size=3D2>This whole issue=20
arises because c# treats objects as references to objects instead as the =
real=20
ones (not that I find this wrong). I think that if you use a language =
that tries=20
to be so simple as c# is, and that tries to "help" the programmer by =
simplifying=20
syntax, etc, then it is suspicious not being able to write a simple =
object=20
assignment&nbsp; (without having both variables referencing the same =
object.=20
Note that by simle I mean using the =3D operator). This could&nbsp;be =
just&nbsp;a=20
decision made by the folks who thought about the language (and then I =
could not=20
make&nbsp;anything about it, of course), so I dont want to make&nbsp;a =
big=20
discussion about this here (because it is really a C# topic and =
not&nbsp;mono=20
specific).</FONT></DIV>
<DIV dir=3Dltr style=3D"MARGIN-RIGHT: 0px"><FONT face=3DArial=20
size=3D2></FONT>&nbsp;</DIV>
<DIV dir=3Dltr style=3D"MARGIN-RIGHT: 0px"><FONT face=3DArial =
size=3D2>Finally, I want=20
to thank you Jonathan, for your comments, I believe they =
helped</FONT></DIV>
<DIV dir=3Dltr style=3D"MARGIN-RIGHT: 0px"><FONT face=3DArial=20
size=3D2></FONT>&nbsp;</DIV>
<DIV dir=3Dltr style=3D"MARGIN-RIGHT: 0px"><FONT face=3DArial =
size=3D2>With=20
regards,</FONT></DIV>
<DIV dir=3Dltr style=3D"MARGIN-RIGHT: 0px"><FONT face=3DArial=20
size=3D2></FONT>&nbsp;</DIV>
<DIV dir=3Dltr style=3D"MARGIN-RIGHT: 0px"><FONT face=3DArial =
size=3D2>Ricardo=20
Kirkner</FONT></DIV></BODY></HTML>

------=_NextPart_000_000C_01C25863.DF2D74E0--