sharpdevelop


Foreach loop VB.Net to C# Code Conversion


Here's the code in VB.Net
If Not queryItems Is Nothing Then
For Each qItem As String In queryItems
qItem = qItem.ToLower()
Next
End If
and it's "equivalent" code in c# (using sharpdevelop/developerfusion/telerik's converter/VS 2012 "paste as c#" method)
if (queryItems != null)
{
foreach (string qItem in queryItems)
{
qItem = qItem.ToLower();
}
}
The C# compiler (rightly so ) complains with the following
"Cannot assign to 'qItem' because it is a 'foreach iteration variable'"
I am wondering why this behavior is permitted in VB.Net?
The crux of the question appears to be this
I am wondering why this behavior is permitted in VB.Net?
A better question may be the following
Why did C# prevent assignment to the foreach iteration variable?
If you look at the set of lopping constructs and languages C# is the odd ball here. In virtually every other case (even in Java foreach) it is legal to assign to the iteration variable of a looping construct. The only other cases I`m aware of are
F#: Variables are readonly by default though so this is just consistency with the rest of the language
Ada: Disallows assignment of the looping value in for statements
VB.Net is actually more consistent here with the norm than C#.
Unfortunately it's not clear why C# made this choice. There are a lot of speculative answers out there but until Eric or Anders does a blog post on this the real reason will remain unknown
It's perfectly reasonable to want to set each string in (an array? a list?) to lower-case, in a loop.
SUGGESTION: just use a good old "for()" loop -
// Assuming array syntax...
if (queryItems != null)
{
for (int i=0; i < queryItems.Length; i++)
{
queryItems[i] = queryItems[i].ToLower();
}
}
IMHO...
PS:
I'm not sure if the VB.Net version ever actually worked as intended. Do you know?

Related Links

The program doesn't show
SharpDevelop and DebugBreak (F12 key)
Disable code inspection in SharpDevelop
The imported project “C:\Dot42.CSharp.targets” was not found
Drawing shapes to use as controls
Using a SharpDevelop library, how do I add a reference to a project?
Foreach loop VB.Net to C# Code Conversion
How to prevent variable substitution in a SharpDevelop project template
How do I highlight syntax errors using ICSharpCode.TextEditor.TextEditorControl?
tell sharpdevelop to use unix line endings?
Active configuration for solutions SharpDevelop
Developing the mono application on Sharp Develop 3.1

Categories

HOME
spring-boot
excel-formula
ssms
kendo-ui-angular2
mobile
virtual-machine
pyspark
service
aix
magento-1.9.2.1
glympse
multipartform-data
handlebars.js
clip-path
pip
obd-ii
digital
select2
repeater
box2d
appsettings
samba
gpo
cherrypy
polybase
connection-pooling
pydub
deferred
directions
eclipse-jdt
jsonapi-resources
incapsula
realloc
framebuffer
idea-plugin
directadmin
php-mongodb
python-venv
gridgain
riemann
aws-rds
hellosign
chain
ios-keyboard-extension
robotc
therubyracer
gevent
firebase-polymer
glade
mongodb-3.3
normalisation
user-profile
jcalendar
cexception
chomsky-normal-form
web-optimization
periodic-processing
abstract
skbio
controlpanel
magnet-uri
event-tracking
gulp-cdnizer
gitk
jain-sip
sparkcore
p4python
page-refresh
stderr
tidesdk
jeet-grid
pymol
javadb
information-hiding
register-allocation
msdropdown
triplestore
dateadd
object-oriented-database
windows-phone-store
surfaceview
ardor3d
mobile-robots
grape-api
kendo-window
http-compression
string-length
auctex
qt5.1
cron4j
openafs
big-endian
blackberry-eclipse-plugin
bash4
mysql-connector
idtabs
mismatch
netduino
hs-err
javah
linq-to-mysql
newspeak
dynamic-scope

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App