技术控

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

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

[复制链接]
不演绎哪来回忆 投递于 2016-10-4 07:38:50
120 1
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:
  1. namespace CsharpStar
  2. {
  3.     public class Program
  4.     {
  5.         public static void Main()
  6.         {
  7.             // Prints a message to the console.
  8.             Console.WriteLine("Hello, World!");
  9.             Console.ReadKey();
  10.         }
  11.     }
  12. }
复制代码
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 :
  1. {
  2.   "version": "1.0.0-*",
  3.   "buildOptions": {
  4.     "debugType": "portable",
  5.     "emitEntryPoint": true
  6.   },
  7.   "dependencies": {},
  8.   "frameworks": {
  9.     "netcoreapp1.0": {
  10.       "dependencies": {
  11.         "Microsoft.NETCore.App": {
  12.           "type": "platform",
  13.           "version": "1.0.0"
  14.         }
  15.       },
  16.       "imports": "dnxcore50"
  17.     }
  18.   }
  19. }
复制代码


  Program.cs :

  1. usingSystem;
  2. namespace ConsoleApplication
  3. {
  4.     public class Program
  5.     {
  6.         public static void Main(string[] args)
  7.         {
  8.             Console.WriteLine("Hello World!");
  9.         }
  10.     }
  11. }
复制代码
     -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
  1. String str ="Hello World !";
  2. //str.Dump();
  3. Console.WriteLine(str);
复制代码
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

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
  1. public class Program
  2. {
  3.     public static void Main()
  4.     {
  5.         System.Console.WriteLine("Hello, world!");
  6.         System.Console.WriteLine("Press any key to exit..");
  7.         System.Console.Read();
  8.     }
  9. }
复制代码
– 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
  1. mcs -out:HelloWorld.exeHelloWorld.cs
复制代码
– You can execute HelloWorld.exe by using below command:
  1. monoHelloWorld.exe
复制代码
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.
  1. usingSystem;
  2. namespace HelloWorld
  3. {
  4.     public class MainClass
  5.     {
  6.         public static void Main(string[] args)
  7.         {
  8.             Console.WriteLine("Hello World!");
  9.             Console.ReadLine();
  10.         }
  11.     }
  12. }
复制代码
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
我也顶起沙发位
回复 支持 反对

使用道具 举报

我要投稿

推荐阅读


回页顶回复上一篇下一篇回列表
手机版/CoLaBug.com ( 粤ICP备05003221号 | 文网文[2010]257号 | 粤公网安备 44010402000842号 )

© 2001-2017 Comsenz Inc.

返回顶部 返回列表