New Apps

Articles

Tips

Events

Improve Soft Skills

/ / / / What are some examples of how C# and Java differ in syntax and capabilities?


C# on the left, Java on the right:


1. Properties: You will immediately notice, that in java you have to write get/set methods instead of properties.

2. Virtual/Final:  Very important difference is, that all methods and properties in java are implicitly virtual. In C# you have to explicitly tell that the property or method is virtual. On the other side, in java, you have to implicitly tell that a method is final (resp, sealed).

3. In C# you can also write properties and method using Lambda syntax (see Test property);

4. In C# you can write optional method parameters. In Java you can achieve the same by method overloading:

Related : To Learn C# fromSratch

5. implicit typing:

  1. //C#:
  2. var person = new Person();
  3. //variable person is of type Person, obviosly
  4.  
  5. //Java
  6. Person person = new Person(); //variable type must be expressed explicitelly


6. Linq:
given that I have following collection of people:

  1. var people = new[]
  2. {
  3. new Person { FirstName = "Daniel", LastName = "Turan" },
  4. new Person { FirstName = "John", LastName = "Smith" },
  5. new Person { FirstName = "John", LastName = "Brown" }
  6. }


in C# i can query against the collection like:

  1. var me = people .Where(p => p.FirstName == "Daniel").First();
  2.  
  3. var numberOfJohns = people.Count(p => p.FirstName == "John");
  4.  
  5. var mostFrequestNames = people
  6. .GroupBy(p => p.FirstName)
  7. .OrderByDescenting(group => group.Count())
  8. .Select(group => new
  9. {
  10. Name = group.Key,
  11. Occurences = group.Count()
  12. });


Linq has fundamentally changes the way we write C# code. In java there is still no alternative that is good enough.

There is much more syntactic sweetness in C#, like async/await, which greatly improves readability and maintainability of asynchronous code, string interpolation, extension method and a lot more.

References :


«
Next

This is the most recent post.

»
Previous

Older Post

About Unknown

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.

No comments :

Leave a Reply

Thursday, September 8, 2016

What are some examples of how C# and Java differ in syntax and capabilities?


C# on the left, Java on the right:


1. Properties: You will immediately notice, that in java you have to write get/set methods instead of properties.

2. Virtual/Final:  Very important difference is, that all methods and properties in java are implicitly virtual. In C# you have to explicitly tell that the property or method is virtual. On the other side, in java, you have to implicitly tell that a method is final (resp, sealed).

3. In C# you can also write properties and method using Lambda syntax (see Test property);

4. In C# you can write optional method parameters. In Java you can achieve the same by method overloading:

Related : To Learn C# fromSratch

5. implicit typing:

  1. //C#:
  2. var person = new Person();
  3. //variable person is of type Person, obviosly
  4.  
  5. //Java
  6. Person person = new Person(); //variable type must be expressed explicitelly


6. Linq:
given that I have following collection of people:

  1. var people = new[]
  2. {
  3. new Person { FirstName = "Daniel", LastName = "Turan" },
  4. new Person { FirstName = "John", LastName = "Smith" },
  5. new Person { FirstName = "John", LastName = "Brown" }
  6. }


in C# i can query against the collection like:

  1. var me = people .Where(p => p.FirstName == "Daniel").First();
  2.  
  3. var numberOfJohns = people.Count(p => p.FirstName == "John");
  4.  
  5. var mostFrequestNames = people
  6. .GroupBy(p => p.FirstName)
  7. .OrderByDescenting(group => group.Count())
  8. .Select(group => new
  9. {
  10. Name = group.Key,
  11. Occurences = group.Count()
  12. });


Linq has fundamentally changes the way we write C# code. In java there is still no alternative that is good enough.

There is much more syntactic sweetness in C#, like async/await, which greatly improves readability and maintainability of asynchronous code, string interpolation, extension method and a lot more.

References :

No comments:

Post a Comment

Social Impact