C# Practical Ans of Zero Lec
C# Practical Ans of Zero Lec
C# Practical Ans of Zero Lec
console application.
using System;
class Program{
using System;
class Program{
Console.WriteLine("Enter a number:");
if (number % 2 == 0){
else{
using System;
class Program
{
int largest = (num1 > num2) ? ((num1 > num3) ? num1 : num3) : ((num2 > num3) ? num2 : num3);
Console.WriteLine($"The largest number among {num1}, {num2}, and {num3} is: {largest}");
using System;
class Program
Console.WriteLine("Enter a year:");
Console.WriteLine($"{year} is {result}");
ASPX:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Registration Form</title>
</head>
<body>
<div>
<h1>Registration Form</h1>
<div>
</div>
<div>
</div>
<div>
<label for="txtEmail">Email:</label>
</div>
<div>
<label for="txtPassword">Password:</label>
</div>
<div>
<label for="ddlCountry">Country:</label>
</asp:DropDownList>
</div>
<div>
</div>
</div>
</form>
</body>
</html>
CSHARP:
using System;
using System.Web.UI;
namespace YourNamespace
if (!IsPostBack)
// Validate and process the registration data (e.g., save to database, send confirmation email, etc.)
using System;
class Program
try
{
Console.WriteLine("Enter a number:");
catch (FormatException)
catch (DivideByZeroException)
finally
Console.WriteLine("End of program.");
ASPX:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Session Example</title>
</head>
<body>
<br />
</div>
</form>
</body>
</html>
CSHARP:
using System;
using System.Web.UI;
namespace YourNamespace
Session["Username"] = username;
if (!IsPostBack)
if (Session["Username"] != null)
{
else
Create a C# console application that prints "Hello, To The Coding World" to the console.
using System;
class Program
Create an ASP.NET Core MVC project with a controller and a view that displays a simple message.
To create an ASP.NET Core MVC project with a controller and a view that displays a simple message,
follow these steps:
2. **Add a Controller**:
In the Solution Explorer, right-click on the "Controllers" folder within your project and select "Add" ->
"Controller".
```csharp
using Microsoft.AspNetCore.Mvc;
namespace YourNamespace.Controllers
return View();
```
4. **Add a View**:
In the Solution Explorer, right-click on the "Views" folder within your project and select "Add" -> "View".
```html
@{
ViewData["Title"] = "Home";
<p>@ViewBag.Message</p>
```
6. **Run the Application**:
Press F5 or click the "Run" button in Visual Studio to build and run your ASP.NET Core MVC application.
- The application will start, and you should see the message "Hello, To The Coding World" displayed on
the homepage.
Implement a sorting algorithm (e.g., Bubble Sort, Quick Sort) in C# for an integer array.
using System;
class Program
int[] numbers = { 5, 2, 9, 1, 5, 6 };
Console.WriteLine("Original Array:");
PrintArray(numbers);
BubbleSort(numbers);
Console.WriteLine("\nSorted Array:");
PrintArray(numbers);
int n = arr.Length;
arr[j + 1] = temp;
{
foreach (int num in arr)
Console.WriteLine();
Write code that intentionally generates an exception and also solve the exception using try and catch.
using System;
class Program
try
int num2 = 0;
Console.WriteLine($"Error: {ex.Message}");
finally
Console.WriteLine("End of program.");
}
Create a simple class with attributes and methods(Class name should be your favorite subject)
using System;
class ComputerScience
// Attributes
// Constructor
SubjectName = subjectName;
Year = year;
Console.WriteLine($"Subject: {SubjectName}");
Console.WriteLine($"Year: {Year}");
if (Year >= 3)
else
Write a C# program to calculate the total marks, percentage and division of two students.
using System;
class Program
// Student 1 data
// Student 2 data
Console.WriteLine("Student 1 Results:");
Console.WriteLine($"Percentage: {student1Percentage}%");
Console.WriteLine($"Division: {student1Division}");
Console.WriteLine("\nStudent 2 Results:");
Console.WriteLine($"Percentage: {student2Percentage}%");
Console.WriteLine($"Division: {student2Division}");
else
return "Fail";
Implement a simple service and use dependency injection to inject it into a controller in an ASP.NET Core
application.
First, create an interface for your service. This interface will define the methods that your service will
implement.
```csharp
string GetMessage();
```
Next, create a class that implements the service interface. This class will contain the implementation of
the service methods.
```csharp
```
3. **Register the Service with Dependency Injection**:
In the `Startup.cs` file of your ASP.NET Core application, register the service with dependency injection
in the `ConfigureServices` method.
```csharp
services.AddSingleton<IMyService, MyService>();
services.AddControllersWithViews();
```
Now, you can inject the service into a controller using constructor injection. Create a controller and
include a constructor that accepts the service interface as a parameter.
```csharp
_myService = myService;
```
Create a view file (`Index.cshtml`) for the controller action. In the view, you can display the message
obtained from the service.
```html
@model string
<h1>@Model</h1>
```
Start your ASP.NET Core application, and navigate to the controller action (e.g.,
`https://localhost:5001/Home/Index`). The view should display the message obtained from the service.
Implement a simple service and use dependency injection to inject it into a controller in an ASP.NET Core
application.
Sure, let's create a simple service and inject it into a controller using dependency injection in an ASP.NET
Core application.
First, create an interface for your service. This interface will define the methods that your service will
implement.
```csharp
string GetMessage();
```
Next, create a class that implements the service interface. This class will contain the implementation of
the service methods.
```csharp
```
In the `Startup.cs` file of your ASP.NET Core application, register the service with dependency injection
in the `ConfigureServices` method.
```csharp
{
services.AddSingleton<IMyService, MyService>();
services.AddControllersWithViews();
```
Now, you can inject the service into a controller using constructor injection. Create a controller and
include a constructor that accepts the service interface as a parameter.
```csharp
_myService = myService;
```
Create a view file (`Index.cshtml`) for the controller action. In the view, you can display the message
obtained from the service.
```html
@model string
<h1>@Model</h1>
```
Start your ASP.NET Core application, and navigate to the controller action (e.g.,
`https://localhost:5001/Home/Index`). The view should display the message obtained from the service.