This week's book giveaway is in the Go forum.
We're giving away four copies of Head First Go and have Jay McGavren on-line!
See this thread for details.
Win a copy of Head First Go this week in the Go forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

Why does C# have many variables types like dynamic,var,int,string instead of having it simple?  RSS feed

 
Ranch Hand
Posts: 1045
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
C# has so many types of variables like dynamic,var,int,string.Why does it not have it simple by having lesser types (e.g say only int, string, char types of variables etc).

thanks
 
Master Rancher
Posts: 58
6
Mac Mac OS X Safari
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"var" is a strongly typed variable, it just assumes the type to be the output of the right-side of the statement.
The compiler isn't confused as to the dataType. So specifying it explicitly it there for humans. The compiler is actually compiling this code.
Thats fine with known types... but there is a reason "var" was introduced with LINQ.

With var it looks a bit cleaner. When you get a more complex transform, the subsequent variable type can get pretty hairy.

When you do "new { <fields> }" you get an anonymous type. It is a readonly class that is automatically named. Still very strongly typed. This helps you when you are pulling data from LINQ, but also in a MVC or WebApi project when you are trying to return JSON to the client.

As a beginner, you should avoid "dynamic". Even experienced people can use it wrong if they don't take the time to understand it. It allows for runtime binding like a scripting language. So when you compile something like this...

The call to "PrintMessage" will compile fine, but at runtime, it will blow up if the underlying method doesn't exist on the object. But it will execute the method if it does exist, regardless of the real type of the object. It also has hooks that will be called if the method is missing. Its kind of like Ruby a bit in that sense. Json-style object-store databases make use of it for dynamic projections also... Like I said, its expert-level stuff.
 
Saloon Keeper
Posts: 1113
36
IBM DB2 Java Netbeans IDE Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One could argue pretty the same about Java. In Java you have int, float, double, char, the corresponding wrapper types, string and string buddies String buffer and string builder and we could go on listing for a while; recently 'var' keyword has been added to the language (var is not a type in both languages, is a placeholder to tell the compiler to infer the actual type of a variable).
I would not call such richness a problem.
 
Monica Shiralkar
Ranch Hand
Posts: 1045
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to understand that either a language needs var which can be used to assign anything or either it needs String, Int , Char etc for the specific types. Buy why both?
 
Claude Moore
Saloon Keeper
Posts: 1113
36
IBM DB2 Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:I am trying to understand that either a language needs var which can be used to assign anything or either it needs String, Int , Char etc for the specific types. Buy why both?


Monica, in the last releaseas Java introduced a lot of changes, many of them being just syntactic sugar.var is among those changes that are essentially a syntactic sugar, i.e a way to write less boilerplate code.Instead of typing, for example,
with var you can simply write


Less chars to type.  The same applies to the for each syntax: you avoid to explicitly iterate over an index, the sintax is clearer and you are protected against index out of bounds errors.
Of course language designers need to be conservative, and older constructs must be supported not to break existing code.
The very same principle applies to C# as well.
 
Bill Crim
Master Rancher
Posts: 58
6
Mac Mac OS X Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:I am trying to understand that either a language needs var which can be used to assign anything or either it needs String, Int , Char etc for the specific types. Buy why both?



Specifying the data type in local variables is redundant in functions where the variable is assigned where it is declared.
 
Monica Shiralkar
Ranch Hand
Posts: 1045
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, what is the advantage which C# gets by having both String/Int/Char and also var compared to languages which have only String/Int/Char like Java and language which have only var like Java Script.
 
Marshal
Posts: 63496
207
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remembering that the relationship between Java® and JavaScript is the same as that between ham and hamster, and remembering that JavaScript sn't a language in the truest sense, why did you ask that last question?
 
Saloon Keeper
Posts: 9869
199
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An advantage of using var is that you don't have to repeat yourself when you're calling a constructor, or when the type is clear from the context:
vs

Some people go overboard with it though. Personally I find it a bit annoying when the type isn't immediately clear from context:

Now, if you're familiar with the code and with the streams API, you will probably correctly guess the type of citiesByCountry, but it's really nice to have a clear visual reminder every now and then:

Regardless, whether you use var or the actual type name, in both cases the variable is statically typed: The compiler knows what the type of the variable is supposed to be, and will check that you use the variable in the way it was intended to be used. When you use an explicit type name, you're telling the compiler: "I expect this variable to have this type". When you use var, you're telling the compiler: "Please determine what the type of this variable is supposed to be, I'm good with whatever you say".

JavaScript is a completely different beast. JavaScript is a dynamically and weakly typed language. Variables don't have a type. You can assign anything to them, you can even reassign them with a value that has a different type than the value they were assigned before. When you use var in JavaScript, it simply means "declare a new local variable", and nothing else. There are no types involved. This is completely different from Java and you can't compare the two. A much better comparison is between var in JavaScript, and dynamic in C#. When you use dynamic in C#, you're telling the compiler: "I don't want you to check the type of this variable at all. Just let me use it how I want". However, at runtime, when you try to access the value in some way that's not valid for its actual type, you will get a runtime error. This is because even though the variable is dynamically typed, C# is still a strongly typed language, and it will not allow you to use a type any other way than it was intended to be used. Again, this is different in JavaScript, which is not only dynamically typed, but also weakly typed. It means you can often treat one type as if it's another type.

For instance, in JavaScript you can subtract one boolean from another boolean as if they were numbers, and the result will be a number.

An advantage of using dynamic variables is that it makes declarations very easy if you don't know or care to figure out ahead of runtime what the type will be, especially when this would involve complex generic types. It has a really really big downside though. The compiler will no longer help you avoid mistakes, and errors may occur when the code is already running in production at a client. Together with weak typing, this makes for the perfect storm because when you wrongly assumed what the type of a variable would be at runtime, JavaScript will still try to interpret it as the type you thought it would be, leading to very subtle issues that are hard to debug.
 
Sheriff
Posts: 21653
101
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Remembering that the relationship between Java® and JavaScript is the same as that between ham and hamster


Or Alf and Gandalf (although I'd like to compare Java to Gandalf, not Alf...).
 
Monica Shiralkar
Ranch Hand
Posts: 1045
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

When you use var, you're telling the compiler: "Please determine what the type of this variable is supposed to be, I'm good with whatever you say".



But what good does it do compared to directly telling the compiler that use this variable for string or use this variable for int.
 
Bill Crim
Master Rancher
Posts: 58
6
Mac Mac OS X Safari
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:

When you use var, you're telling the compiler: "Please determine what the type of this variable is supposed to be, I'm good with whatever you say".



But what good does it do compared to directly telling the compiler that use this variable for string or use this variable for int.

whyrell the compiler something it already knows? Also, when using an anonymous or dynamic type, the programmer doesn’t know the type name up front.
 
Stephan van Hulst
Saloon Keeper
Posts: 9869
199
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:But what good does it do compared to directly telling the compiler that use this variable for string or use this variable for int.


Did you see my first example? It's just a way of not repeating yourself when the type is clear. Programming is as much poetry as it is a way to get things done.
 
Monica Shiralkar
Ranch Hand
Posts: 1045
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all.

why tell the compiler something it already knows? Also, when using an anonymous or dynamic type, the programmer doesn’t know the type name up front.



If my understanding is correct instead of writing we can write var The only advantage is that we do not have to write MyClass object on the left hand side and can easily write var.
 
Ranch Hand
Posts: 277
2
Fedora Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pretty sure java has var now too
 
Monica Shiralkar
Ranch Hand
Posts: 1045
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry I mistyped. I the correct one is as below


 
Stephan van Hulst
Saloon Keeper
Posts: 9869
199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's still not correct, because the class name is MyClass, and you need to call constructors with parentheses:
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!