DEV Community

Calin Baenen
Calin Baenen

Posted on

Can I apply my Java knowledge to C#?

I was just wondering what about Java can and can not be applied to C#.
I know they have a lot of similarities, but what are some of the key differences, and what are the "key similarities"?

Also, one question I definitely want answered is: "I learned how to compile C# using csc test.cs, but, how is the main class chosen?".

Top comments (3)

Collapse
 
slavius profile image
Slavius • Edited

Java and C# unfortunately have less and less common over the years. Nowadays they differ in almost everything. The only principles you can apply in C# you already know are not Java specific - like inheritance, polymorphism, inversion of control, interfaces, etc.
From my own experience writing applications in C# is far less code and more syntactic sugar than Java.
.Net framework has most of the stuff you need and if you need something more it's most of the time provided as NuGet packages by Microsoft while NuGet package management is part of the framework. No more messing with Maven, Nexus and 5 different Java packages for a simple requirement.
The best starting point would be. Net Core framework, which has CLI tool dotnet that allows you to bootstrap a C# project or solution in a second.
By default C# program is searching for void Main() method and executes it first but this may differ for different project types (class library vs. console program vs. web application).
It is advised you create new project using dotnet new command which creates project_name.csproj file which eases running it by using dotnet run command.
If you already have built .dll file you can use dotnet path/to/the/file.dll any arguments to run it.

Collapse
 
baenencalin profile image
Calin Baenen

C#.... web application?

Collapse
 
slavius profile image
Slavius • Edited

Yes, part of the .Net (Core) framework is an application server named Kestrel. The most simple C# web application looks like this:
Program.cs

namespace test2
{
    public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                });
    }
}
Enter fullscreen mode Exit fullscreen mode

Startup.cs

namespace test2
{
    public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddRazorPages();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
            }

            app.UseStaticFiles();
            app.UseRouting();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapRazorPages();
            });
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

test2.csproj

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
  </PropertyGroup>
</Project>
Enter fullscreen mode Exit fullscreen mode