• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 119
8
Mac Mac OS X Safari
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 1357
39
IBM DB2 Netbeans IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 1357
39
IBM DB2 Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Rancher
Posts: 119
8
Mac Mac OS X Safari
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 15510
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Rancher
Posts: 119
8
Mac Mac OS X Safari
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 15510
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 667
14
TypeScript Fedora
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pretty sure java has var now too
 
Monica Shiralkar
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry I mistyped. I the correct one is as below


 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's still not correct, because the class name is MyClass, and you need to call constructors with parentheses:
reply
    Bookmark Topic Watch Topic
  • New Topic