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:
- //C#:
- var person = new Person();
- //variable person is of type Person, obviosly
- //Java
- Person person = new Person(); //variable type must be expressed explicitelly
6. Linq:
given that I have following collection of people:
- var people = new[]
- {
- new Person { FirstName = "Daniel", LastName = "Turan" },
- new Person { FirstName = "John", LastName = "Smith" },
- new Person { FirstName = "John", LastName = "Brown" }
- }
in C# i can query against the collection like:
- var me = people .Where(p => p.FirstName == "Daniel").First();
- var numberOfJohns = people.Count(p => p.FirstName == "John");
- var mostFrequestNames = people
- .GroupBy(p => p.FirstName)
- .OrderByDescenting(group => group.Count())
- .Select(group => new
- {
- Name = group.Key,
- Occurences = group.Count()
- });
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.
No comments :