Skip to main content

Posts

Showing posts with the label csharp

How to do Alignment within string.Format in C#

Align String with Spaces [C#] This example shows how to align strings with spaces. The example formats text to table and writes it to console output. To align string to the right or to the left use static method String.Format. To align string to the left (spaces on the right), use negative number like below String.Format("{0,–10}", text) To align string to the right (spaces on the left), use postive number like below String.Format("{0,10}", text) Following example shows how to format text to the table. Values in the first and second column are aligned to the left and the third column is aligned to the right. Console.WriteLine("-------------------------------"); Console.WriteLine("First Name | Last Name | Age"); Console.WriteLine("-------------------------------"); Console.WriteLine(String.Format("{0,-10} | {1,-10} | {2,5}", "Adam", "Gilchrist", 50)); Console.WriteLine(String.Format("{0,-10

First C# Program

C# can be used in a window-based, web-based, or console application. To start with, we will create a console application to work with C#.  Open Visual Studio (2019 or later) installed on your local machine. Click on File -> New Project... as below       From the New Project popup, select C# in the language menu and select the Console App in the project type menu and click next.       In the name section, give any appropriate project name, a location where you want to create your project files.     Click next and select target framework.   Click Create to create the console application.   Program.cs will be created as default a C# file in Visual Studio where you can write your C# code in Program class, as shown above.   To see the output of the above C# program, we have to compile it and run it by pressing Ctrl + F5 or clicking the "HelloWorld" Run button.