An Overview Of ScriptCS

Last week, I attended C# Corner annual conference. In this conference, I met MVPs, authors, speakers, and the C# Corner Team. In this conference I learned a lot. I met with Mr. Glenn Block and I learned some new technologies, and ScriptCS is one of them.
 
What is ScriptCS?

ScriptCS
 enables developers to write C# code and C# applications using any text editor, and the C# Code compilation is performed by Roslyn. We can manage all packages and dependencies using Nuget.

Mr. Glenn Block, (Principal Product Manager at Splunk) developed the ScriptCS project to make C# a scripting language.  
 
How to Install ScriptCS: The following steps will explain how you can you install ScriptCS.
  1. Before installing ScriptCS you need to install Chocolatey.

    You can install Chocolatey using your command prompt. Open command prompt as administrator and then just copy the following command and paste in command prompt. 
    1. @powershell -NoProfile -ExecutionPolicy Bypass -Command "iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))" && SET PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin  
    You can also install Chocolatey using PowerShell. To install use PowerShell using the following command:
    1. iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))  
  2. After installing Chocolatey, you can install the latest stable version of ScriptCS from your command prompt using the following command. 
    1. cinst scriptcs  
    Chocolatey will install ScriptCS to "%APPDATA%\scriptcs\". But if you want to update ScriptCS path then you can update accordingly.

    Note:
    After installing you may need to restart your computer. 
Any time you want to update ScriptCS then you can update using the following command:
  1. cup scriptcs   
How to Create and Run a C# Code using ScriptCS

To create and run a C# Code using ScriptCS is very easy as in the following:
  1. Open Command Prompt.

  2. Type "scriptcs" command in command prompt and hit Enter key.



  3. Now write your code as in the following:

     
Working with variables using ScriptCS,

 

 Working with loops using ScriptCS,

 

How to run C# Code after writing in your favorite text editor

You can write your C# code in your favorite text editor and save it as .csx
 
Example: Open your favorite text editor and write the following code:
  1. for(int i=0;i<10;i++)  
  2. {  
  3.      for(int j=0;j<i;j++)  
  4.      {  
  5.            Console.Write("*");  
  6.      }     
  7.      Console.WriteLine();  
  8. }  
Now save this code as Program.csx. and run this using the following command:
  1. scriptcs Program.csx  
Output

  
Conclusion

In this article we have learned the following things:
  • About ScriptCS 
  • Installation of Chocolaty
  • Installation of ScriptCS
  • Run simple program using ScriptCS
  • Working with variables 
  • Working with loops 
  • We have also seen how we can write our C# code in your favorite text editor and run that code.
Read more articles on C# Programming:

Up Next
    Ebook Download
    View all
    Learn
    View all
    sourabhsomani.com