技术控

    今日:57| 主题:49101
收藏本版 (1)
最新软件应用技术尽在掌握

[其他] Five ways to create your first C# Program

[复制链接]
不演绎哪来回忆 发表于 2016-10-4 07:38:50
73 1

立即注册CoLaBug.com会员,免费获得投稿人的专业资料,享用更多功能,玩转个人品牌!

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
In this article, you will learn: 5 different ways to create your first C# program

  
       
  • Create a new Console Application   
  • Create a new program using ASP.net Core   
  • Create a new query using LINQPad   
  • Create a new program using Mono   
  • Create a new project using Xamarin Studio  
  1. Create a new Console Application :

  To create a basic console application:
  – Open Visual Studio
  – In the toolbar, go to File → New Project
  – Select the Console Application project type
  – Add the below code and then hit CTRL+F5 to run the program:
  [code]namespace CsharpStar
{
    public class Program
    {
        public static void Main()
        {
            // Prints a message to the console.
            Console.WriteLine("Hello, World!");
            Console.ReadKey();
        }
    }
}
[/code]  Output:

  Hello, World!
  Description:

  – class ‘Program’ is a class declaration and it contains the data and method definitions that your program uses. In above example,the Program class has only one method: Main.
  – static void Main() defines the Main method, which is the entry point for all C# programs.Only one Main method is allowed per class.
  – Console.WriteLine(“Hello, world!”); method prints a given string as an output in the console window.
  – Console.ReadKey(), ensures that the program won’t close immediately after displaying the message.
  2. Create a new program using ASP.net Core :

      – Install the .NET Core SDK for your platform:
   – Windows
   – OSX
   – Linux
   – After the installation has completed, open a command prompt. Either bash or powershell.
– Create a new directory with   mkdir hello_world   
    – Change into the newly created directory with cd hello_world .
    – Create a new console application with dotnet new -t console .
   This will produce two files:
    project.json : [code]{
  "version": "1.0.0-*",
  "buildOptions": {
    "debugType": "portable",
    "emitEntryPoint": true
  },
  "dependencies": {},
  "frameworks": {
    "netcoreapp1.0": {
      "dependencies": {
        "Microsoft.NETCore.App": {
          "type": "platform",
          "version": "1.0.0"
        }
      },
      "imports": "dnxcore50"
    }
  }
}
[/code]


  Program.cs :

  [code]usingSystem;

namespace ConsoleApplication
{
    public class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}
[/code]      -Restore the needed packages: dotnet restore .
    – Build the application with dotnet build -c Release .
– Run the application with   dotnet run ./bin/Release/netcoreapp1.0/hello_world.dll   
   
    Output:

  Hello, World!
   Creating a new program using ASP.net Core in Windows: you can readhere.
  3. Create a new query using LINQPad:

   – Download and Install LINQPad
  – Create a new Query by CTRL+N or File->New Query
  – Under Language, select C# statement or C# Program
  – Write the below code and hit F5
  [code]String str ="Hello World !";
//str.Dump();
Console.WriteLine(str);
[/code]  Output:

  Hello, World!
  – If you click on “IL”, you can inspect the IL code that your .net code generates.
   
Five ways to create your first C# Program-1 (different,message,article,create,public)

  – Using IL, you can see the SQL that is being generated when using LINQ to SQL.
  4. Create a new program using Mono:

   – Download and Install Mono
  – After successful installation, create a text file, name it HelloWorld.cs
  [code]public class Program
{
    public static void Main()
    {
        System.Console.WriteLine("Hello, world!");
        System.Console.WriteLine("Press any key to exit..");
        System.Console.Read();
    }
}
[/code]  – If you are using Windows, run the Mono Command Prompt which is included in the Mono installation and ensures that the necessary environment variables are set. If on Mac or Linux, open a new terminal.
  – Compile the newly created file and it will produce HelloWorld.exe
  [code]mcs -out:HelloWorld.exeHelloWorld.cs
[/code]  – You can execute HelloWorld.exe by using below command:
  [code]monoHelloWorld.exe
[/code]  Output:

  Hello, World!
  5. Create a new project using Xamarin Studio:

   – Download and Install Xamarin Studio
  – Open Xamarin Studio
  – Click File->New->Solution
  – Click .NET -> Console Project and choose C#.
  – Click Next to proceed.
  – Enter the project name and browse the location to save the project.
  – Add the below code to program.cs and Hit play button or F5 to run the code.
  [code]usingSystem;

namespace HelloWorld
{
    public class MainClass
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            Console.ReadLine();
        }
    }
}
[/code]  Output:

  Hello, World!
  Summary:

  In this article, we have learned 5 different ways to create our first C# program.
  
       
  • Create a new Console Application   
  • Create a new program using ASP.net Core   
  • Create a new query using LINQPad   
  • Create a new program using Mono   
  • Create a new project using Xamarin Studio  
  Thanks for visiting !!
   © 2016,CsharpStar. All rights reserved.
友荐云推荐




上一篇:Renewed as an MVP for Visual Studio ALM for the 5th Year (2016)
下一篇:Simon Riggs: Cool Runnings
酷辣虫提示酷辣虫禁止发表任何与中华人民共和国法律有抵触的内容!所有内容由用户发布,并不代表酷辣虫的观点,酷辣虫无法对用户发布内容真实性提供任何的保证,请自行验证并承担风险与后果。如您有版权、违规等问题,请通过"联系我们"或"违规举报"告知我们处理。

福旦达 发表于 2016-11-14 21:39:39
我也顶起沙发位
回复 支持 反对

使用道具 举报

*滑动验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

我要投稿

推荐阅读

扫码访问 @iTTTTT瑞翔 的微博
回页顶回复上一篇下一篇回列表手机版
手机版/CoLaBug.com ( 粤ICP备05003221号 | 文网文[2010]257号 )|网站地图 酷辣虫

© 2001-2016 Comsenz Inc. Design: Dean. DiscuzFans.

返回顶部 返回列表