
Top .NET Programming Examples Every Developer Should Know
.NET has become one of the most versatile and robust programming frameworks used by developers across the globe. Whether you’re building web applications, desktop software, or mobile apps, .NET offers a powerful set of tools and libraries to make development easier and more efficient. In this blog, we’ll explore some essential .NET programming examples that every developer should know to fully leverage the capabilities of this framework.
Introduction to .NET Programming
The .NET framework, developed by Microsoft, provides a comprehensive environment for building a wide range of applications. From web apps to desktop software, .NET offers support for multiple programming languages such as C#, F#, and VB.NET. Developers prefer .NET due to its flexibility, security features, and support for cross-platform development.
When learning .NET, having real-world .NET programming examples is essential. These examples help developers understand the potential of the framework and how it can be applied to different project types.
Basic .NET Programming Example: Hello World
One of the first programs that any developer writes is the classic “Hello World” application. This is a great starting point for learning the structure of .NET applications.
using System;
namespace HelloWorldApp
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(“Hello, World!”);
}
}
}
This simple program demonstrates the basic syntax of C# and .NET. Here, we are using the Console.WriteLine function to print “Hello, World!” to the console. Key components include the Main method, which is the entry point of the program, and the namespace keyword, which helps organize code into logical units.
Building a Simple Console Application in .NET
Console applications are some of the most straightforward programs you can develop in .NET. These are command-line programs that take input, process it, and display output. Below is an example of a simple console application that performs basic arithmetic operations.
using System;
namespace CalculatorApp
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(“Enter first number:”);
int num1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(“Enter second number:”);
int num2 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(“Sum: ” + (num1 + num2));
}
}
}
This application prompts the user to enter two numbers and then outputs their sum. It demonstrates how to use input/output operations, data conversion, and basic arithmetic in .NET.
Developing a Web Application Using ASP.NET
ASP.NET is one of the most popular frameworks for developing web applications within the .NET ecosystem. Here’s a simple .NET programming example of building a basic web application using ASP.NET.
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Hosting;
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapGet(“/”, () => “Welcome to ASP.NET Core Web Application!”);
app.Run();
This code creates a minimal web application using ASP.NET Core. It uses the MapGet method to map the root URL (/) to return a welcome message. ASP.NET is highly scalable, making it ideal for building anything from small websites to large-scale enterprise web applications.
Creating a Windows Desktop Application with .NET
.NET also allows developers to build desktop applications using technologies like Windows Forms and WPF (Windows Presentation Foundation). Here’s a simple example of creating a desktop application using Windows Forms:
using System;
using System.Windows.Forms;
namespace DesktopApp
{
public class MainForm : Form
{
public MainForm()
{
Button helloButton = new Button();
helloButton.Text = “Click Me”;
helloButton.Click += HelloButton_Click;
Controls.Add(helloButton);
}
private void HelloButton_Click(object sender, EventArgs e)
{
MessageBox.Show(“Hello, World!”);
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new MainForm());
}
}
}
This Windows Forms application creates a simple window with a button. When the user clicks the button, a message box appears displaying “Hello, World!” Desktop applications like these are perfect for users who need rich, interactive experiences on their computers.
Integrating a Database in .NET Applications
One of the most common tasks in application development is working with databases. .NET offers several tools for database integration, including Entity Framework (EF). Below is an example of how to perform CRUD (Create, Read, Update, Delete) operations in a .NET application using Entity Framework:
public class Product
{
public int ProductId { get; set; }
public string Name { get; set; }
}
public class ApplicationDbContext : DbContext
{
public DbSet<Product> Products { get; set; }
}
class Program
{
static void Main(string[] args)
{
using (var context = new ApplicationDbContext())
{
var product = new Product { Name = “Sample Product” };
context.Products.Add(product);
context.SaveChanges();
}
}
}
This code demonstrates how to define a Product entity, set up a database context, and insert a product into the database using Entity Framework.
Building a RESTful API with .NET
RESTful APIs are crucial for modern web applications, and .NET makes it easy to build them with ASP.NET Core. Below is an example of a simple REST API:
[ApiController]
[Route(“api/[controller]”)]
public class ProductsController : ControllerBase
{
private static List<Product> products = new List<Product>
{
new Product { ProductId = 1, Name = “Product 1” },
new Product { ProductId = 2, Name = “Product 2” }
};
[HttpGet]
public IEnumerable<Product> Get()
{
return products;
}
[HttpPost]
public IActionResult Post(Product product)
{
products.Add(product);
return Ok(product);
}
}
This API allows for retrieving and adding products. The [HttpGet] and [HttpPost] attributes define the HTTP methods used to interact with the API.
Using .NET for Cross-Platform Mobile Development
With the help of Xamarin, .NET developers can create cross-platform mobile applications. Xamarin enables you to write native Android and iOS apps using C# and .NET. Here’s a simple example:
using Xamarin.Forms;
public class App : Application
{
public App()
{
MainPage = new ContentPage
{
Content = new Label
{
Text = “Hello Xamarin!”,
VerticalOptions = LayoutOptions.CenterAndExpand,
HorizontalOptions = LayoutOptions.CenterAndExpand,
},
};
}
}
This app creates a simple “Hello Xamarin!” label that is displayed in the center of the screen. Xamarin’s ability to share code across platforms makes it an excellent tool for mobile development.
.NET and Internet of Things (IoT) Development
.NET is increasingly being used in IoT (Internet of Things) projects due to its flexibility and scalability. Here’s an example of .NET in an IoT scenario, where the application monitors sensor data:
using System;
using System.Device.Gpio;
class Program
{
static void Main(string[] args)
{
var pin = 17;
var controller = new GpioController();
controller.OpenPin(pin, PinMode.Input);
while (true)
{
var value = controller.Read(pin);
Console.WriteLine($”Pin Value: {value}”);
System.Threading.Thread.Sleep(1000);
}
}
}
This program reads sensor data from a GPIO pin and prints the values to the console, which can be useful for monitoring temperature, humidity, or other environmental factors.
Conclusion
Mastering .NET involves understanding the vast array of application types it supports. Whether you’re building console applications, web apps, or even IoT solutions, the framework offers powerful tools and libraries to bring your ideas to life. By studying and implementing the .NET programming examples discussed in this blog, developers can sharpen their skills and build more efficient and scalable applications.