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
autohotkey
gcc
ssms
leaflet
jenkins-pipeline
phantomjs
watson-iot
msbi
opacity
pdf-generation
rethinkdb
cracking
ios-universal-links
google-app-maker
aws-sdk-cpp
civicrm
descriptor
monad-transformers
opengraph
fastboot
advertising
uipath
struts
datastax-enterprise
my.cnf
sasl
inspec
visual-studio-community
csproj
sphinx4
opencsv
jcr
voyager
openvms
respect-validation
gstreamer-1.0
abide
stochastic-process
console.readline
oracle-bpm-suite
nscollectionview
mockserver
observer-pattern
ng-lightning
android-graphview
mtm
libzip
niagara-ax
hdpi
import.io
px4
playframework-1.x
seyren
r-commander
boost-regex
senti-wordnet
jtextarea
pyautogui
git-cherry-pick
archiva
factorization
alchemy
yfiles
computability
gnu-sort
magnet-uri
mojolicious
webservices-client
gitk
database-project
hexagonal-tiles
photogrammetry
genome
mri
esri-arc-engine
qxorm
aiff
information-hiding
illegalargumentexception
lwuit
javacompiler
indic
disparity-mapping
internet-radio
kernel-density
imaplib
google-voice
aspnet-compiler
usersettings
architectural-patterns
auctex
vline
volatility
tasklist
big-endian
jquery-1.8
github-archive
flatten
django-admin-tools
mvcmailer
fileoutputstream
cherokee
jexcelapi
llblgen
glassfish-embedded
uccapi
coderush-xpress
authkit
bootstrap-protocol
cstring
jgrowl

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