Certainly! Here’s a simple example of a C# program that calculates the sum of two numbers entered by the user:
using System; class Program { static void Main() { Console.WriteLine("Enter the first number:"); int number1 = int.Parse(Console.ReadLine()); Console.WriteLine("Enter the second number:"); int number2 = int.Parse(Console.ReadLine()); int sum = number1 + number2; Console.WriteLine("The sum of the numbers is: " + sum); } }
In this example, we start by prompting the user to enter the first number. We use Console.ReadLine()
to read the input as a string, and then we use int.Parse()
to convert it to an integer.
Next, we do the same for the second number.
After that, we calculate the sum by adding number1
and number2
, and store the result in the sum
variable.
Finally, we display the sum to the user using Console.WriteLine()
.
You can run this program in a C# development environment such as Visual Studio or by using the .NET command-line tools.
C# Example: Hello World
Certainly! Here’s a simple example of a C# program that prints “Hello, World!” to the console:
using System; class Program { static void Main() { Console.WriteLine("Hello, World!"); } }
In this example, we have a class named Program
with a Main
method. The Main
method is the entry point of the program, where the execution starts.
Inside the Main
method, we use Console.WriteLine()
to print the string “Hello, World!” to the console. The Console.WriteLine()
method writes the specified string followed by a new line character.
You can run this program in a C# development environment such as Visual Studio or by using the .NET command-line tools. When you run the program, it will display “Hello, World!” on the console output.
C# Simple Example:
Certainly! Here’s a simple example of a C# program that calculates the area of a rectangle:
using System; class Program { static void Main() { double length = 5.0; double width = 3.0; double area = length * width; Console.WriteLine("The area of the rectangle is: " + area); } }
In this example, we have a class named Program
with a Main
method.
Inside the Main
method, we define two variables length
and width
and assign them the values 5.0 and 3.0, respectively. These variables represent the length and width of the rectangle.
Next, we calculate the area of the rectangle by multiplying the length and width together and store the result in the area
variable.
Finally, we use Console.WriteLine()
to display the calculated area to the console.
When you run this program, it will output “The area of the rectangle is: 15” because the length is 5 and the width is 3, resulting in an area of 15 square units.
C# Example: Using System
Certainly! Here’s a simple example of a C# program that demonstrates the usage of the System
namespace:
using System; class Program { static void Main() { // Example of using System.Math double radius = 5.0; double circumference = 2 * Math.PI * radius; Console.WriteLine("The circumference of the circle is: " + circumference); // Example of using System.DateTime DateTime currentDate = DateTime.Now; Console.WriteLine("The current date and time is: " + currentDate); } }
In this example, we have a class named Program
with a Main
method.
Inside the Main
method, we demonstrate the usage of two components from the System
namespace.
First, we use the System.Math
component to calculate the circumference of a circle. We define a variable radius
with a value of 5.0 and calculate the circumference using the formula 2 * Math.PI * radius
. The Math.PI
constant represents the mathematical constant pi. We then display the circumference using Console.WriteLine()
.
Next, we use the System.DateTime
component to retrieve the current date and time. We assign the value of DateTime.Now
to the variable currentDate
and display it using Console.WriteLine()
.
By importing the System
namespace at the beginning of the program, we gain access to a wide range of classes, methods, and constants provided by the .NET Framework.
C# Example: Using public modifier
Certainly! Here’s a simple example of a C# program that demonstrates the usage of the public
modifier:
using System; class Program { public static void Main() { Calculator calculator = new Calculator(); int result = calculator.Add(5, 3); Console.WriteLine("The result of addition is: " + result); result = calculator.Subtract(10, 4); Console.WriteLine("The result of subtraction is: " + result); } } public class Calculator { public int Add(int a, int b) { return a + b; } public int Subtract(int a, int b) { return a - b; } }
In this example, we have a class named Program
with a public static
Main
method. This method serves as the entry point of the program, where the execution starts.
Inside the Main
method, we create an instance of the Calculator
class and store it in the calculator
variable.
We then use the calculator
object to perform addition and subtraction operations by calling the Add
and Subtract
methods defined in the Calculator
class. Both of these methods are marked with the public
modifier, which means they can be accessed from outside the class.
The Add
method takes two integers as parameters and returns their sum, while the Subtract
method takes two integers as parameters and returns their difference.
Finally, we display the results of the addition and subtraction operations using Console.WriteLine()
.
By using the public
modifier, we make the methods accessible to other parts of the program, allowing them to be invoked from the Main
method in this case.
C# Example: Using namespace
Certainly! Here’s a simple example of a C# program that demonstrates the usage of namespaces:Certainly! Here’s a simple example of a C# program that demonstrates the usage of namespaces:
using System; namespace MyNamespace { class Program { static void Main() { Console.WriteLine("Hello, World!"); } } }
In this example, we have a namespace named MyNamespace
that contains a class named Program
.
Inside the Program
class, we have a Main
method. The Main
method is the entry point of the program, where the execution starts.
By including the using System;
statement at the top of the code, we import the System
namespace. This allows us to use classes and members from the System
namespace without fully qualifying their names.
For instance, in the Main
method, we can directly use Console.WriteLine()
instead of System.Console.WriteLine()
.
The namespace serves as a container for logically organizing related classes and prevents naming conflicts between different namespaces.
You can include multiple using
statements to import different namespaces or even create your own namespaces to group related classes and avoid naming clashes within your own code.