如何从 Java 迁移到 C#?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/212263/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 18:19:32  来源:igfitidea点击:

How do I move from Java to C#?

c#java

提问by Steve McLeod

I know Java well. Which caveats and resources will help me cross to the other side (C#) as painlessly as possible.

我很了解Java。哪些注意事项和资源将帮助我尽可能轻松地跨越到另一边 (C#)。

采纳答案by Jon Skeet

Biggest tip: go with the .NET naming conventionsfrom the word go. That way you'll constantly be reminded about which language you're in. (Sounds silly, but it really is helpful.) Embrace the idioms of the language as far as possible.

最大的提示:使用 go一词中的.NET 命名约定。这样你就会不断地被提醒你使用的是哪种语言。(听起来很傻,但确实很有帮助。)尽可能地接受该语言的习语。

There are various books specifically for folks in your situation - search for "C# for Java" in Amazon and you'll get plenty of hits. It's worth reading carefullyto make sure you don't assume that things will work the same in C# as in Java. (For instance, in C# instance variable initializers are executed before the base class constructor body; in Java they happen after. Subtle things like this can take a while to learn, and are easy to miss if you're skimming.)

有各种专门针对您这种情况的人的书籍 - 在 Amazon 中搜索“C# for Java”,您将获得大量点击。值得仔细阅读以确保您不会假设在 C# 中的工作方式与在 Java 中的工作方式相同。(例如,在 C# 中,实例变量初始化器在基类构造函数体之前执行;在 Java 中,它们发生在之后。像这样的微妙事情可能需要一段时间才能学习,如果你略读,很容易错过。)

If you're going to be using C# 3, I'd get a book which definitely covers that - everything in C# 3 will be new to you. Gratuitous plug: my own book (C# in Depth) covers C# 2 and 3, but assumes you already know C# 1. (In other words, it won't be enough on its own, but you may want it as a "second" book.)

如果您打算使用 C# 3,我会得到一本书,其中肯定会涵盖这一点 - C# 3 中的所有内容对您来说都是新的。免费插件:我自己的书(深入了解 C#)涵盖了 C# 2 和 3,但假设您已经了解 C# 1。(换句话说,仅靠它是不够的,但您可能希望将其作为“第二个”书。)

回答by Jorge Ferreira

See this great article on C# from a Java Developer's Perspective. It has several insights on the things that can be done in both sides to avoid minimum overhead. Having example in both the language you know and the language you want to learn eases the learning curve quite a bit.

从 Java Developer's Perspective 看这篇关于C# 的精彩文章。它对双方可以做的事情有几个见解,以避免最小的开销。在你知道的语言和你想学习的语言中都有例子可以大大缓解学习曲线。

回答by Paco

The language syntax is vary similar, so I should only read a small reference of the C# syntax. Like a simple book (for experienced programmers) or maybe wikipedia (http://en.wikipedia.org/wiki/Comparison_of_Java_and_C_Sharp) will tell enough.

语言语法各不相同,所以我应该只阅读 C# 语法的一小部分参考。就像一本简单的书(对于有经验的程序员)或者维基百科(http://en.wikipedia.org/wiki/Comparison_of_Java_and_C_Sharp)就足够了。

The biggest difference is the library: Asp.Net websites are totally different from java servlets.

最大的区别在于库:Asp.Net 网站与 java servlet 完全不同。

Don't read much, just start programming!

不用看太多,直接开始编程吧!

回答by Paco

Here's a link that has syntax comparison between Java and C# (even though it's almost identical, there are a few differences).

这是一个链接,其中包含 Java 和 C# 之间的语法比较(尽管它几乎相同,但也存在一些差异)。

http://www.harding.edu/fmccown/java1_5_csharp_comparison.html

http://www.harding.edu/fmccown/java1_5_csharp_comparison.html

回答by Scott Cowan

  1. Install Visual Studio 2008 and Resharper with IntelliJ IDEA key bindings. This gives you things like prompting you to include namespaces if you start using them.
  2. Start a new project and start writing Java code, when you run into something that doesn't work properly or it's unable to find the class you're trying to use Google "PrintLn in c#".
  3. Write tests or code snippets for sanity checks, like you may want to check if == works for strings (it does)
  4. realize that c# alias Data Types (int is an alias for System.Int32, string for System.String)
  5. look at other peoples code I recommend JP Boodhoos Google code
  6. Take a job in C#, there's lots of jobs requiring both Java and C# especially in support.
  7. Know your libraries, most Java libraries have been ported and most of the time the name is either like (Hibernate => NHibernate) or (Xstream => Xstream.Net). Not every library has an obvious name so just start looking into random ones you hear about here. ie (Rhino.Mocks,HTMLAgilityPack,MBUnit,Rhino.Commons,Castle Project)
  8. Go to usergroup meetings look for a DNUG (Dot Net User Group) they'll be helpful and you can get some good advice.
  1. 使用 IntelliJ IDEA 键绑定安装 Visual Studio 2008 和 Resharper。如果您开始使用命名空间,这会提示您包含命名空间。
  2. 启动一个新项目并开始编写 Java 代码,当您遇到无法正常工作或无法找到您尝试使用 Google“PrintLn in c#”的类时。
  3. 为健全性检查编写测试或代码片段,就像您可能想检查 == 是否适用于字符串(确实如此)
  4. 意识到 c# 别名数据类型(int 是 System.Int32 的别名,System.String 的字符串)
  5. 看别人的代码 我推荐JP Booodhoos 谷歌代码
  6. 找一份 C# 的工作,有很多工作需要 Java 和 C#,尤其是在支持方面。
  7. 了解您的库,大多数 Java 库已被移植,并且大多数情况下名称类似于 (Hibernate => NHibernate) 或 (Xstream => Xstream.Net)。不是每个图书馆都有一个明显的名字,所以只要开始研究你在这里听到的随机图书馆。即(Rhino.Mocks、HTMLAgilityPack、MBUnit、Rhino.Commons、Castle Project)
  8. 参加用户组会议,寻找 DNUG(Dot Net 用户组),他们会有所帮助,您可以获得一些好的建议。

回答by axelclk

Use Sharpento convert your Java programs to C# and see the differences.

使用Sharpen将您的 Java 程序转换为 C# 并查看差异。

回答by mezoid

I know that a good answer has already been accepted. However, I'd like to make an addition...

我知道一个好的答案已经被接受。不过,我想补充一点……

I find that learning a new language typically involves learning subtle syntactic differences....especially when dealing with the difference between languages in the C/C++/Java/C# family.

我发现学习一门新语言通常涉及学习细微的句法差异……尤其是在处理 C/C++/Java/C# 家族中语言之间的差异时。

In addition to a nice thick reference book I recommend getting a pocket reference like C# 3 Pocket Reference from O'Reilly. It won't help you with the design patterns etc...but will provide a very quickreference about the specific differences of the language you are using.

除了一本很厚的参考书外,我还建议您从 O'Reilly获得一本袖珍参考书,例如C# 3 Pocket Reference。它不会帮助您处理设计模式等……但会提供有关您所使用语言的具体差异的非常快速的参考。

Here's a quick blurb about this book from that site:

以下是该网站上关于这本书的简要介绍:

C# 3.0 Pocket Reference includes plenty of illustrations and code examples to explain:

  • Features new to C# 3.0, such as lambda expressions, anonymous types, automatic properties, and more
  • All aspects of C# syntax, predefined types, expressions, and operators
  • Creating classes, structs, delegates and events, enums, generics and constraints, exception handling, and iterators
  • The subtleties of boxing, operating overloading, delegate covariance, extension method resolution, interface reimplementation, nullable types, and operating lifting
  • LINQ, starting with the principles of sequences, deferred execution and standard query operators, and finishing with a complete reference to query syntax-including multiple generators, joining, grouping, and query continuations
  • Consuming, writing, and reflecting on custom attributes

C# 3.0 Pocket Reference 包含大量插图和代码示例来解释:

  • C# 3.0 的新特性,例如 lambda 表达式、匿名类型、自动属性等
  • C# 语法、预定义类型、表达式和运算符的所有方面
  • 创建类、结构、委托和事件、枚举、泛型和约束、异常处理和迭代器
  • 装箱、操作重载、委托协变、扩展方法解析、接口重新实现、可为空类型和操作提升的微妙之处
  • LINQ,从序列、延迟执行和标准查询运算符的原则开​​始,最后是对查询语法的完整参考——包括多个生成器、连接、分组和查询延续
  • 使用、编写和反思自定义属性

I used this book (well the original) to help me go from being a Java to a C# developer. While I was learning, I kept it by my desk at all times and it really helped.

我使用这本书(以及原版)帮助我从 Java 成为 C# 开发人员。在我学习的时候,我一直把它放在我的办公桌旁,它真的很有帮助。

回答by Tony Peterson

I made the transition pretty easily by using C# at work, but one of the most important things to do is familiarize yourself with the .NET API and some of the powerful techniques available in C#.

我通过在工作中使用 C# 很容易地完成了转换,但最重要的事情之一是熟悉 .NET API 和 C# 中可用的一些强大技术。

After I learned the .net library I relied on it a lot more than I used to, so learning about the things it can do for you is very helpful. After that, if you work with db code at all, learn LINQ, and also techniques lambas, anonymous types and delegates are also a useful to pick up.

在我学习了 .net 库之后,我比以前更加依赖它,因此了解它可以为您做的事情非常有帮助。之后,如果您完全使用 db 代码,请学习 LINQ,并且还可以学习 Lambas、匿名类型和委托技术。

回答by Saikat

There's a cheat-sheetfrom Microsoft for Java developers using C# :)

Microsoft 为使用 C# 的 Java 开发人员提供了一份备忘单:)