473,326 Members | 2,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

C# Language Suggestions

Hello,

I understand that this is a good place to post suggestions
about future changes to the language, and that the
Microsoft developers who develop C# read this newsgroup.
If there is a more appropriate place to post this, please
let me know. So, here are a few things I'd like to see.

1. A way to declare a local variable as being readonly.

2. A way to define static variables that are only
accessible from within a given function, like in C++.
This is a nice form of information hiding, helps put
definitions near their use, and reduces clutter in the
class.
Andrew

Nov 15 '05 #1
11 1967
Andrew,
So, here are a few things I'd like to see.

1. A way to declare a local variable as being readonly.

2. A way to define static variables that are only
accessible from within a given function, like in C++.


I am not against the results achieved in an application from these two
suggestions.

However, I wonder why changes to the C# language are required?

versus

Designing your application to provide this functionality?

For example, for a readonly local variable -- I assume this means that its
value is constant because it is readonly. Then, why not implement a method
that returns the constant value? If you want to set it once, why not
implement a method that uses lazy initialization? Or why not ...? Which
approach one takes depends on what qualities one really needs to have.

For statics only accessible in a given function -- why not design the
application to do just that? Only access the statics from that function.

I guess I don't see that the complexity of additional syntax is balanced by
the value of the functionality. It must be my evil Smalltalk side showing
through. Smalltalk is a pure OO language that has only a hand-full of
syntax and keywords (10 or so) to learn.

It takes some real good syntax to beat no syntax at all!

Regards,

Randy
Nov 15 '05 #2
"Andrew" <as********@nospam.onezero.org> wrote in message
news:00****************************@phx.gbl...
1. A way to declare a local variable as being readonly.
Isn't that a constant?
2. A way to define static variables that are only
accessible from within a given function, like in C++.
This is a nice form of information hiding, helps put
definitions near their use, and reduces clutter in the
class.


That would be good.

Regards,
Michael Culley
Nov 15 '05 #3
Hi,

This is a good place to start, but Iwould also suggest you to go to
http://www.gotdotnet.com/team/csharp...e/default.aspx there you
will see the new proposed features of the language

1. A way to declare a local variable as being readonly. Like what?
If you mean the const modificator to a parameter it has been discussed here
before, take a look at the archives:
2. A way to define static variables that are only
accessible from within a given function, like in C++.
This would be a good idea, but if take a second think about it you see that
the static functionality was inherited from C as a way of encapsulation,
there the other possible way of a variable was global, in a OOP language you
can define a private member of the class, I know it's not the same but IMHO
it's enough , un less that you have a big class or several developers
working in the same class it should be ok.
I think that the most difficult decision in designing a language is not
what include but what is left out ( I remember I hear this phrase from my
programming language's teacher in the univ, Miguel Katrib ) , if you include
all the functionality that other languages have you will end with a fat
compiler and a difficult language.

those were my two cents ;)

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Andrew" <as********@nospam.onezero.org> wrote in message
news:00****************************@phx.gbl... Hello,

I understand that this is a good place to post suggestions
about future changes to the language, and that the
Microsoft developers who develop C# read this newsgroup.
If there is a more appropriate place to post this, please
let me know. So, here are a few things I'd like to see.

1. A way to declare a local variable as being readonly.

2. A way to define static variables that are only
accessible from within a given function, like in C++.
This is a nice form of information hiding, helps put
definitions near their use, and reduces clutter in the
class.
Andrew

Nov 15 '05 #4
>Michael Culley wrote:
Isn't that a constant?


It's a constant in the English language sense. It is not
a constant in another sense: in C# the "const" keyword
indicates something that is fully evaluatable at compile-
time, whereas the "readonly" keyword indicates only that
the value cannot be changed after initialization.
The "readonly" keyword already is part of the language and
can be used for class fields; you can't currently
use "readonly" with local variables. You can use "const"
with local variables.
Nov 15 '05 #5
check out the readonly keyword. You can define a variable that can only be
written to as a part of the definition or during the class constructor.

As for your second suggestion, I miss this too.

"Andrew" <as********@nospam.onezero.org> wrote in message
news:00****************************@phx.gbl...
Hello,

I understand that this is a good place to post suggestions
about future changes to the language, and that the
Microsoft developers who develop C# read this newsgroup.
If there is a more appropriate place to post this, please
let me know. So, here are a few things I'd like to see.

1. A way to declare a local variable as being readonly.

2. A way to define static variables that are only
accessible from within a given function, like in C++.
This is a nice form of information hiding, helps put
definitions near their use, and reduces clutter in the
class.
Andrew

Nov 15 '05 #6
<"Martin Robins" <martin - robins @ ntlworld dot com>> wrote:
check out the readonly keyword. You can define a variable that can only be
written to as a part of the definition or during the class constructor.


That's for class and instance variables, not local variables as the OP
requested.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #7


--
Eric Gunnerson

Visit the C# product team at http://www.csharp.net
Eric's blog is at http://blogs.gotdotnet.com/ericgu/

This posting is provided "AS IS" with no warranties, and confers no rights.
"Andrew" <as********@nospam.onezero.org> wrote in message
news:00****************************@phx.gbl...
Hello,

I understand that this is a good place to post suggestions
about future changes to the language, and that the
Microsoft developers who develop C# read this newsgroup.
If there is a more appropriate place to post this, please
let me know. So, here are a few things I'd like to see.

1. A way to declare a local variable as being readonly.
I don't think I've heard of this before. Do you have a scenario in mind?
2. A way to define static variables that are only
accessible from within a given function, like in C++.
This is a nice form of information hiding, helps put
definitions near their use, and reduces clutter in the
class.


There are two reasons we don't do this.

The first is that you can get the same effect by using a class static, so
adding support for method statics would not enable a new scenario and would
make the language more complex.

The second - and more important reason, in my book - is that method-level
statics have a tendency to bite you if you ever end up writing things to be
thread-safe. In C++, for example, you have to read the entire class to be
sure that there isn't a method static in use.
Nov 15 '05 #8
Hi Eric.
--- Local readonly Variables

The benefits of compiler support for annotating local
variables with the "readonly" keyword are essentially the
same as from using "readonly" with class fields, and are
the same as having "const" local variables in C++.
Probably a lot has been written about this type of "const"
for C++. One reference is the book "C++ FAQs", Second
Edition, by Marshall Cline, Greg Lomow, and Mike Girou. I
can write a bit here too about benefits
of "const" / "readonly".

The readonly keyword in a variable definition saves the
human reader and the compiler from having to do a code
analysis to realize that the variable does not change
during its instantiations. Instead of doing the analysis,
the reader can just see the readonly keyword and
immediately realize that the variable will never change
after it's initialized.

Using readonly also can make the programmer's intent
clearer.

For example, if a variable x is declared as readonly, and
at some future date someone adds another assignment to x,
the compiler will catch this and emit an error. This can
be valuable because the code may rely in a non-obvious way
on a value being invariant.

A specific situation where "readonly" is useful is in a
series of calculations where one is building up a series
of results that depend on previous results. For example,
in the code below, the variable "dh" is used in several
places, and once we see that "dh" is readonly, we
immediately know that all references to dh refer to the
same value. Without the "readonly" keyword we would have
to work to obtain this information, or possibly we would
not even be aware of it.

protected override void OnPaint(PaintEventArgs pea)
{
Graphics g = pea.Graphics;
readonly int cw = QMath.IRound
(g.VisibleClipBounds.Width);
readonly int ch = QMath.IRound
(g.VisibleClipBounds.Height);

// logo

readonly int lx = (cw-d_logo.Width)/2;
readonly int ly = d_logo.Height / 4;
readonly int lBottomB = ly+d_logo.Height;
g.DrawImageUnscaled(d_logo,lx,ly);

// dialog

readonly int dh = d_dialogPanel.CalculateHeight();
readonly int dvMarginMin = ly;
readonly int dhMargin = dvMarginMin;
readonly int dwMin = d_logo.Width;
readonly int dw = Math.Max(dwMin, cw-2*dhMargin);

readonly int dvMargin = Math.Max(dvMarginMin, (ch-
lBottomB-dh)/2);
readonly int dTop = lBottomB + dvMargin;
readonly int dLeft = (cw-dw)/2;
readonly int dRight = dLeft + dw -1;
readonly int dBottom = dTop + dh - 1;

d_dialogPanel.SuspendLayout();
d_dialogPanel.Left = dLeft;
d_dialogPanel.Top = dTop;
d_dialogPanel.Height = dh;
d_dialogPanel.Width = dw;
d_dialogPanel.ResumeLayout();

base.OnPaint(pea);
}

Another kind of common case occurs around loops, say like
this:

...
readonly int x = something;
for (int i = ...) {
int y = f(x,i);
some code
readonly z = g(x,i)
some code
y = h(z,y)
etc
}

When attempting to comprehend code like this it's very
helpful to know that x is a loop invariant.

With C++ functions written by someone else it's sometimes
helpful to try putting "const" in front of some variables
and compiling, rather than manually determining whether
those variables are assigned twice. Using "const" can be
useful in other kinds of refactoring activities as well.

I've found the C++ "const" especially useful with
mathematical code. When a function is doing a complicated
operation, having "const" keywords can have a big impact
on clarity and intent. I do not think that readonly's
usefulness is limited to mathematical code.

I like the benefits of "const"/"readonly" so much that my
own preference is to use these keywords in almost every
definition that allows it.
--- Function-Static Variables:

The argument that class-static variables already exist and
therefore function-static don't add new functionality does
not make sense to me, because the same argument can also
be used to say that the language should not have local
variables because class fields already exist.

Class-static variables, as supported now in C#, aren't any
more thread safe than function static variables. I
understand what's meant about having to look for function-
static variables in order to make a class thread safe.
This doesn't seem like a very strong argument, since in
order to make a class thread safe one has to look at each
function anyway to know that what it's doing is thread
safe. In order to have thread safety it's necessary to
understand the data flow, and that is not possible without
knowledge of where variables are defined and initialized.
If one knows these things then one will know that a
variable is static. In other words, unless one is hacking
randomly hoping to stumble upon thread safety, one will
have to know a variable is static in order to get thread
safety, so having the variable be function-static should
not impede thread safety any more than having a variable
be class-static would.

In some cases having a static variable be local to a
function will help during the process of making the class
be thread safe, because the "static" keyword will make it
clear that the variable's use is localized to the
function, or, at a minimum, that all uses of that variable
can be traced to that function. Knowing these things is
helpful when trying to understand the data flow.

Andrew
Nov 15 '05 #9
Answers inline

--
Eric Gunnerson

Visit the C# product team at http://www.csharp.net
Eric's blog is at http://blogs.gotdotnet.com/ericgu/

This posting is provided "AS IS" with no warranties, and confers no rights.
"Andrew" <as********@nospam.onezero.org> wrote in message
news:1a*****************************@phx.gbl...
Hi Eric.
--- Local readonly Variables

The benefits of compiler support for annotating local
variables with the "readonly" keyword are essentially the
same as from using "readonly" with class fields, and are
the same as having "const" local variables in C++.
Probably a lot has been written about this type of "const"
for C++. One reference is the book "C++ FAQs", Second
Edition, by Marshall Cline, Greg Lomow, and Mike Girou. I
can write a bit here too about benefits
of "const" / "readonly".

The readonly keyword in a variable definition saves the
human reader and the compiler from having to do a code
analysis to realize that the variable does not change
during its instantiations. Instead of doing the analysis,
the reader can just see the readonly keyword and
immediately realize that the variable will never change
after it's initialized.

Using readonly also can make the programmer's intent
clearer.

For example, if a variable x is declared as readonly, and
at some future date someone adds another assignment to x,
the compiler will catch this and emit an error. This can
be valuable because the code may rely in a non-obvious way
on a value being invariant.

A specific situation where "readonly" is useful is in a
series of calculations where one is building up a series
of results that depend on previous results. For example,
in the code below, the variable "dh" is used in several
places, and once we see that "dh" is readonly, we
immediately know that all references to dh refer to the
same value. Without the "readonly" keyword we would have
to work to obtain this information, or possibly we would
not even be aware of it.

protected override void OnPaint(PaintEventArgs pea)
{
Graphics g = pea.Graphics;
readonly int cw = QMath.IRound
(g.VisibleClipBounds.Width);
readonly int ch = QMath.IRound
(g.VisibleClipBounds.Height);

// logo

readonly int lx = (cw-d_logo.Width)/2;
readonly int ly = d_logo.Height / 4;
readonly int lBottomB = ly+d_logo.Height;
g.DrawImageUnscaled(d_logo,lx,ly);

// dialog

readonly int dh = d_dialogPanel.CalculateHeight();
readonly int dvMarginMin = ly;
readonly int dhMargin = dvMarginMin;
readonly int dwMin = d_logo.Width;
readonly int dw = Math.Max(dwMin, cw-2*dhMargin);

readonly int dvMargin = Math.Max(dvMarginMin, (ch-
lBottomB-dh)/2);
readonly int dTop = lBottomB + dvMargin;
readonly int dLeft = (cw-dw)/2;
readonly int dRight = dLeft + dw -1;
readonly int dBottom = dTop + dh - 1;

d_dialogPanel.SuspendLayout();
d_dialogPanel.Left = dLeft;
d_dialogPanel.Top = dTop;
d_dialogPanel.Height = dh;
d_dialogPanel.Width = dw;
d_dialogPanel.ResumeLayout();

base.OnPaint(pea);
}

Another kind of common case occurs around loops, say like
this:

...
readonly int x = something;
for (int i = ...) {
int y = f(x,i);
some code
readonly z = g(x,i)
some code
y = h(z,y)
etc
}

When attempting to comprehend code like this it's very
helpful to know that x is a loop invariant.

With C++ functions written by someone else it's sometimes
helpful to try putting "const" in front of some variables
and compiling, rather than manually determining whether
those variables are assigned twice. Using "const" can be
useful in other kinds of refactoring activities as well.

I've found the C++ "const" especially useful with
mathematical code. When a function is doing a complicated
operation, having "const" keywords can have a big impact
on clarity and intent. I do not think that readonly's
usefulness is limited to mathematical code.

I like the benefits of "const"/"readonly" so much that my
own preference is to use these keywords in almost every
definition that allows it.
I don't have any real comments here.


--- Function-Static Variables:

The argument that class-static variables already exist and
therefore function-static don't add new functionality does
not make sense to me, because the same argument can also
be used to say that the language should not have local
variables because class fields already exist.
I don't agree. Local variables and class fields are very different beasts,
with locals being associated with the function and instance ones associated
with the instace (somehow that seems like a tautology).

Or, to put it another way, you can get the identical effect by using a
class-level const/readonly as you could over one declared in the function.
It would mean the same thing. It's really just a question of where you get
to declare them.
Class-static variables, as supported now in C#, aren't any
more thread safe than function static variables. I
understand what's meant about having to look for function-
static variables in order to make a class thread safe.
This doesn't seem like a very strong argument, since in
order to make a class thread safe one has to look at each
function anyway to know that what it's doing is thread
safe. In order to have thread safety it's necessary to
understand the data flow, and that is not possible without
knowledge of where variables are defined and initialized.
If one knows these things then one will know that a
variable is static. In other words, unless one is hacking
randomly hoping to stumble upon thread safety, one will
have to know a variable is static in order to get thread
safety, so having the variable be function-static should
not impede thread safety any more than having a variable
be class-static would.

In some cases having a static variable be local to a
function will help during the process of making the class
be thread safe, because the "static" keyword will make it
clear that the variable's use is localized to the
function, or, at a minimum, that all uses of that variable
can be traced to that function. Knowing these things is
helpful when trying to understand the data flow.


That would be the same as if it was declared as static to the class. My
point is that it's easier if all the class-level variables are centralized
rather than being spread out in various methods.

The complexity argument is at least as important as this one, however. Since
you can get the same effect with a class-level static, you don't need to
have method-level statics, and we generally try not to add a feature that
duplicates something that's already there.

Eric.
Nov 15 '05 #10
>> (local readonly discussion)
I don't have any real comments here.
So What's the next step?

I don't agree. Local variables and class fields are very different beasts, with locals being associated with the function and instance ones associated with the instace (somehow that seems like a tautology).

Or, to put it another way, you can get the identical effect by using a class-level const/readonly as you could over one declared in the function. It would mean the same thing. It's really just a question of where you get to declare them.
(This part of the discussion was about function-static
variables, not "const/readonly" -- I guess you
meant "static" here instead of "const/readonly".)

As I understood it, the argument that was presented
against having function-static variables was: if some
thing X already can be done at the class level, then one
doesn't get anything new by having X be done locally in a
function. My point was that there is something wrong with
this argument because if one takes X="variable" then the
argument is claiming that nothing new is obtained by
having local variables (since one could use class fields
instead of local variables), and yet C# has local
variables.

Information hiding is the reason that I'm suggesting this
feature.

That would be the same as if it was declared as static to the class. My point is that it's easier if all the class-level variables are centralized rather than being spread out in various methods.
As I discussed, I do not agree that the case of a thread-
safe application is easier without function-static
variables, and I have not heard any other cases. So I
don't understand what it is that's easier if function-
static doesn't exist. My personal preference would be to
localize the information to get information hiding, not to
put all the information at a level where it's accessible
to entities that have no business accessing it.

The complexity argument is at least as important as this one, however. Since you can get the same effect with a class-level static, you don't need to have method-level statics, and we generally try not to add a feature that duplicates something that's already there.


I am happy that C# has gone in the direction of trying to
keep things simple. That is one of the things I like
about the language, and I would not want things to be
otherwise. The new capability that function-static would
add is a form of information hiding that is not present
with class-static variables. Simplicity is one design
criterion, and information hiding is another criterion,
and information hiding has precedent in C#, e.g., there
are no global variables, classes can have private and
protected variables, functions can have local variables,
and "for" and "foreach" variable scope is limited to the
loop. These things aren't functionally necessary, except
for local variables (which also provide information
hiding).

Nov 15 '05 #11
Andrew <as********@nospam.onezero.org> wrote:
As I understood it, the argument that was presented
against having function-static variables was: if some
thing X already can be done at the class level, then one
doesn't get anything new by having X be done locally in a
function. My point was that there is something wrong with
this argument because if one takes X="variable" then the
argument is claiming that nothing new is obtained by
having local variables (since one could use class fields
instead of local variables), and yet C# has local
variables.


How exactly would you propose using class variables instead of local
variables? Bear in mind that:

o There may be multiple threads running a single method at a time
o A single thread may have recursively called the same method several
times

Presumably it would be possible to mimic local variables through the
use of ThreadStaticAttribute and storing everything within Stacks, etc
- but that's a far cry from the simplicity with which you can mimic
"method-local statics".

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: R. Rajesh Jeba Anbiah | last post by:
I'm supposed to do a big portal with multi-language support and I'm decided to do my own instead of going for gettext. I have sort out few implementations; most of them uses global variables. I'm...
12
by: mpinsley | last post by:
We are a software company that provides Inventory & Procurement mangement to the hospitality industry. For the past twenty years we have been using Progress Software as both the development...
16
by: Ding Lei | last post by:
Dear fellows, I am currently a Java programmer, using it for around 3 years, & felt quite bored with it. IMHO, Java is too strict on lots of things, unlike Perl, There is usually only one or two...
22
by: Michael Nahas | last post by:
Antti & all interested, The draft description of my language to replace C is available at: http://nahas.is-a-geek.com/~mike/MyC.pdf I am a long time C programmer (I read the old testament...
2
by: mpinsley | last post by:
We are a software company that provides Inventory & Procurement mangement to the hospitality industry. For the past twenty years we have been using Progress Software as both the development...
7
by: Robert Seacord | last post by:
The CERT/CC has just deployed a new web site dedicated to developing secure coding standards for the C programming language, C++, and eventually other programming language. We have already...
84
by: aarklon | last post by:
Hi all, I found an interesting article here:- http://en.wikipedia.org/wiki/Criticism_of_the_C_programming_language well what do you guys think of this article....??? Is it constructive...
3
Banfa
by: Banfa | last post by:
The project I work on has a bespoke hardware platform which is designed to go into a variety of different situations. However to keep things simple we really want the software for the platform to...
4
by: edgy | last post by:
Hello, I am a beginner with PHP, and I have made a language switcher on a site that I am looking for your feedback on. First of all, the page is http://www.mankar.ca My question regarding...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.