An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
Hi @Gurdeep Vaidya ,
Thanks for reaching out.
You can use the following small examples as a starting point. They are kept intentionally simple, but each one shows a practical pattern you would use in a real C# application.
For an ASP.NET Core MVC example, you could have a controller prepare some data and pass it to a Razor view like this:
public class Product
{
public int Id { get; set; }
public string Name { get; set; } = string.Empty;
public decimal Price { get; set; }
}
using Microsoft.AspNetCore.Mvc;
public class ProductsController : Controller
{
public IActionResult Index()
{
var products = new List<Product>
{
new Product { Id = 1, Name = "Keyboard", Price = 49.99m },
new Product { Id = 2, Name = "Mouse", Price = 19.99m }
};
return View(products);
}
}
Then the Razor view can receive that model and display it:
@model List<Product>
<h2>Products</h2>
<table>
<thead>
<tr>
<th>Name</th>
<th>Price</th>
</tr>
</thead>
<tbody>
@foreach (var product in Model)
{
<tr>
<td>@product.Name</td>
<td>@product.Price.ToString("C")</td>
</tr>
}
</tbody>
</table>
In this case, when the user browses to /Products, the Index() action creates a small product list and passes it to the view for rendering.
For Windows Forms, a common basic example is handling a button click and validating user input before showing a result:
using System;
using System.Windows.Forms;
public class MainForm : Form
{
private readonly TextBox nameTextBox;
private readonly Button submitButton;
private readonly Label resultLabel;
public MainForm()
{
Text = "Windows Forms Example";
Width = 350;
Height = 200;
nameTextBox = new TextBox
{
Left = 20,
Top = 20,
Width = 200
};
submitButton = new Button
{
Left = 20,
Top = 60,
Width = 100,
Text = "Submit"
};
resultLabel = new Label
{
Left = 20,
Top = 100,
Width = 300
};
submitButton.Click += SubmitButton_Click;
Controls.Add(nameTextBox);
Controls.Add(submitButton);
Controls.Add(resultLabel);
}
private void SubmitButton_Click(object? sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(nameTextBox.Text))
{
MessageBox.Show("Please enter your name.");
return;
}
resultLabel.Text = $"Hello, {nameTextBox.Text}!";
}
}
The application entry point would look like this:
using System;
using System.Windows.Forms;
internal static class Program
{
[STAThread]
private static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
}
This creates a simple form with a textbox, a button, and a label. When the button is clicked, the code checks whether the textbox has a value before updating the label.
For unit testing, here is a small MSTest example. First, this is the class being tested:
public class Calculator
{
public int Add(int a, int b)
{
return a + b;
}
}
And this is a matching test class:
using Microsoft.VisualStudio.TestTools.UnitTesting;
[TestClass]
public class CalculatorTests
{
[TestMethod]
public void Add_WhenCalled_ReturnsSum()
{
var calculator = new Calculator();
int result = calculator.Add(2, 3);
Assert.AreEqual(5, result);
}
}
If you are using MSTest, make sure the test project has the usual test packages installed, for example:
<PackageReference Include="MSTest.TestFramework" Version="3.4.3" />
<PackageReference Include="MSTest.TestAdapter" Version="3.4.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
For a GoF design pattern, a simple and useful one is the Factory pattern. It helps you create objects without spreading new SomeConcreteClass() throughout the application:
public interface INotificationSender
{
void Send(string message);
}
public class EmailNotificationSender : INotificationSender
{
public void Send(string message)
{
Console.WriteLine($"Email sent: {message}");
}
}
public class SmsNotificationSender : INotificationSender
{
public void Send(string message)
{
Console.WriteLine($"SMS sent: {message}");
}
}
public static class NotificationSenderFactory
{
public static INotificationSender Create(string type)
{
return type.ToLower() switch
{
"email" => new EmailNotificationSender(),
"sms" => new SmsNotificationSender(),
_ => throw new ArgumentException("Unknown notification type")
};
}
}
You can then use it like this:
INotificationSender sender = NotificationSenderFactory.Create("email");
sender.Send("Your order has been shipped.");
The benefit here is that the calling code works with the INotificationSender interface instead of depending directly on a specific implementation such as EmailNotificationSender or SmsNotificationSender. That makes the code easier to extend later if you add another sender type.
Hope this helps! If my answer was helpful, I would greatly appreciate it if you could follow the instructions here so others with the same problem can benefit as well.