| Download the amazing global Makindo app: Android | Apple | |
|---|---|
| MEDICAL DISCLAIMER: Educational use only. Not for diagnosis or management. See below for full disclaimer. |
Related Subjects: |Python |C |Java |C++ |C sharp |VB |Natural Language processing |How does a CPU work |Computer Networking |Computer Security |Concurrent Programming |Compilers |Cryptography |Data Structures |Database Management |Object orientated programming
C# (pronounced "C-sharp") is a modern, object-oriented programming language developed by Microsoft. It is part of the .NET framework and is used for developing a wide range of applications, from desktop and web applications to mobile and cloud-based applications. C# combines the power of C++ with the simplicity of Visual Basic, making it a versatile and easy-to-learn language.
// This is a single-line comment
/* This is a multi-line comment spanning multiple lines */
////// This is a documentation comment. ///
using System;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
}
}
}
int age = 25; float height = 5.9f; double pi = 3.14159; char initial = 'A'; string name = "Alice"; bool isActive = true;
if (age > 18)
{
Console.WriteLine("Adult");
}
else
{
Console.WriteLine("Minor");
}
switch (initial)
{
case 'A':
Console.WriteLine("Grade A");
break;
case 'B':
Console.WriteLine("Grade B");
break;
default:
Console.WriteLine("Other Grade");
break;
}
for (int i = 0; i < 5; i++)
{
Console.WriteLine(i);
}
int j = 0;
while (j < 5)
{
Console.WriteLine(j);
j++;
}
int k = 0;
do
{
Console.WriteLine(k);
k++;
} while (k < 5);
static int Add(int a, int b)
{
return a + b;
}
static void Main(string[] args)
{
int sum = Add(5, 3);
Console.WriteLine("Sum: " + sum);
}
static void Greet(string name)
{
Console.WriteLine("Hello, " + name + "!");
}
static void Main(string[] args)
{
Greet("Alice");
}
class Dog
{
public string Name;
public int Age;
public Dog(string name, int age)
{
Name = name;
Age = age;
}
public void Bark()
{
Console.WriteLine(Name + " says woof!");
}
}
class Program
{
static void Main(string[] args)
{
Dog myDog = new Dog("Buddy", 3);
myDog.Bark();
}
}
class Animal
{
public string Name;
public virtual void Speak()
{
Console.WriteLine("Animal speaks");
}
}
class Dog : Animal
{
public override void Speak()
{
Console.WriteLine(Name + " says woof!");
}
}
class Program
{
static void Main(string[] args)
{
Dog myDog = new Dog();
myDog.Name = "Buddy";
myDog.Speak();
}
}
class Animal
{
public virtual void Speak()
{
Console.WriteLine("Animal speaks");
}
}
class Dog : Animal
{
public override void Speak()
{
Console.WriteLine("Dog barks");
}
}
class Cat : Animal
{
public override void Speak()
{
Console.WriteLine("Cat meows");
}
}
class Program
{
static void Main(string[] args)
{
Animal myDog = new Dog();
Animal myCat = new Cat();
myDog.Speak(); // Output: Dog barks
myCat.Speak(); // Output: Cat meows
}
}
try
{
int result = 10 / 0;
}
catch (DivideByZeroException e)
{
Console.WriteLine("Cannot divide by zero!");
}
try
{
int result = 10 / 0;
}
catch (DivideByZeroException e)
{
Console.WriteLine("Cannot divide by zero!");
}
finally
{
Console.WriteLine("This will always be printed");
}
using System;
using System.Data.SqlClient;
class Program
{
static void Main(string[] args)
{
string connectionString = "Data Source=(local);Initial Catalog=MyDatabase;Integrated Security=True";
string query = "SELECT * FROM MyTable";
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
SqlCommand command = new SqlCommand(query, connection);
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
Console.WriteLine(reader["ColumnName"].ToString());
}
}
}
}
using System;
using System.Net.Http;
using System.Threading.Tasks;
class Program
{
static async Task Main(string[] args)
{
string url = "https://jsonplaceholder.typicode.com/todos/1";
string result = await FetchDataAsync(url);
Console.WriteLine(result);
}
static async Task FetchDataAsync(string url)
{
using (HttpClient client = new HttpClient())
{
HttpResponseMessage response = await client.GetAsync(url);
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync();
}
}
}
using System;
using System.Windows.Forms;
public class MainForm : Form
{
private Button button;
public MainForm()
{
button = new Button();
button.Text = "Click me";
button.Click += new EventHandler(Button_Click);
Controls.Add(button);
}
private void Button_Click(object sender, EventArgs e)
{
MessageBox.Show("Button clicked!");
}
[STAThread]
public static void Main()
{
Application.Run(new MainForm());
}
}
using System;
using System.Windows;
namespace WpfApp
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Button clicked!");
}
}
}
C# is a powerful and versatile programming language used for developing a wide range of applications. Understanding its basic syntax, control structures, object-oriented principles, error handling, data access techniques, asynchronous programming, and GUI development is essential for building robust and efficient software. C#'s integration with the .NET framework makes it a popular choice for developers working on Windows-based applications.