Skip to main content

Posts

Showing posts with the label c#

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

Access Modifiers in C#

How many access modifiers in c# In this article, we will learn about the access modifiers in C#. 1. Public Access Modifier in C# The class member, that is defined as a Public can be accessed by other class members that are initialized outside the class. A public member can be accessed from anywhere even outside the namespace. public class Vehicle { public void Color() { Console.WriteLine("Vehicle color red"); } } 2. Private Access Modifier in C# The private access modifier restrict the member variable or function to be called outside of the parent class. A private function or variable cannot be called outside of the same class. It hides its member variable and method from other class and methods. public class Vehicle { private string Name; public void Color() { Console.WriteLine("Vehicle color red"); } } In the above example, you cannot call name variable outside the class because it is declared as private. 3. Protected

What is the sealed class in C#

In this article, we will learn about the sealed class and method in C#. C# Sealed Class A sealed class, in C#, is a class that cannot be inherited by any class but can be initiated. When we don't want a class can be inherited by another class, We can declare that class as sealed class. A sealed class cannot have a derived class. We use the sealed keyword to create a sealed class. sealed class Vehicle { } class Car : Vehicle { } class Program { public static void Main(string[] args) { Car car = new Car(); Console.ReadLine(); } } In the above example, we have created a sealed class Vehicle and then we are trying to derived Car class from the Vehicle class. when we are trying to compile code it'll give following errors.       C# Sealed Method When we don't want method can be override by another class, We can declare that method as sealed method. We use a sealed keyword with an overridden method to create a sealed method. class Vehicle

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.