Beginner's guide to c#
Introduction
C# (pronounced 'C-sharp') is a modern, object-oriented programming language developed by Microsoft. It's a core component of the .NET framework.
Classes and Objects
Just like in many other object-oriented languages, classes and objects are at the heart of C#:
public class Dog {
public string name;
public void Bark() {
Console.WriteLine(name + " says woof!");
}
}
Dog myDog = new Dog();
myDog.name = "Buddy";
myDog.Bark();
Variables and Data Types
C# offers a variety of built-in data types:
int number = 5;
double decimalNumber = 5.5;
char character = 'A';
string text = "Hello, World!";
bool isTrue = false;
Conditional Statements
Make decisions in your code with if, else if, and else statements:
int x = 10;
if (x > 5) {
Console.WriteLine("x is greater than 5");
} else if (x == 5) {
Console.WriteLine("x is equal to 5");
} else {
Console.WriteLine("x is less than 5");
}
Loops
Execute code multiple times using loops like for and while:
for (int i = 0; i < 5; i++) {
Console.WriteLine(i);
}
int j = 0;
while (j < 5) {
Console.WriteLine(j);
j++;
}
Methods
Methods allow you to encapsulate and reuse logic:
public int Add(int a, int b) {
return a + b;
}
int result = Add(2, 3);
Console.WriteLine(result); // Outputs 5
Conclusion
This is just the tip of the iceberg when it comes to C#. With its robust libraries and extensive features, C# is a powerful tool for developers. Dive deeper and explore more to truly harness its potential!