Written by {Code} with Abhishek Luv , Saturday, April 9, 2016 at 3:34 PM
Learning C# for beginners can be little difficult in the first place. But it isn’t that difficult.
When starting to learn C# you should start with the basics and not get into the deep rabbit hole of it.
This is Day 1 of Learn C# in 7 Days.
Let’s begin…
Every programming language is made up by using words and letters known as Keywords.
While programming we use letters
and words
to form expressions and statements
and then those statements
can be used within isolated function bodies called as methods
.
class ExampleHelloWorld
{
static void Main()
{
Console.WriteLine("Hello World");
}
}
In the above given example, class
, static
, void
are keywords and Main()
is a method defined by the end user which acts as the entry point for the HelloWorld
program.
C# is an object-oriented programming language. We won’t be going into the topic of Classes
and Objects
as of now.
Let’s dissect the above give example.
Console
is a C# class coming from the System
namespace.
Object-oriented programming is about creating objects from classes and then using variables and calling methods of those objects.
Similarly, in the above code example, Console
is a class which has a method called WriteLine
which prints a value or any data on the console.
Now, How and what will the WriteLine
method print on the console? It won’t do anything on its own.
We need to instruct the WriteLine
method what to print on the screen and we can do that by passing data to the method known as arguments
.
Console.WriteLine("Hello World");
in the above line of code we are passing a string
i.e. “Hello World” to the WriteLine
method. It’s the job of the WriteLine
method to print the given data via arguments on the console screen.
Code without comments and documentation is bad code.
Comments are lines of text which gives information about the flow of the program and what does a method do when it’s called.
There are three types of comments:
Single-Line Comments
// this is variable with int data type
int myAge = 28;
Multi-Line Comments
/* This is a multi-line comment
You can write whatever you want
*/
XML Documentation Comments
/// <summary>
/// This is the Main Method. It is an entry point for our application
/// </summary>
static void Main()
{
string firstName = "Abhishek";
string lastName = " Luv";
string fullName = firstName + lastName;
Console.WriteLine(fullName);
}
XML documentation comments are used to write documentations for classes and methods in C#.
These XML comments are then used by Visual Studio to provide us information using Intellisense.
To write an XML comment just press /
3 times on any class or method.
Every program needs memory and space to store data before it performs any operations and actions on the data.
Every value has a data type associated with it.
For example:
Abhishek
will be of type string
in C#12345
will be of type int
in C#bool
in C#233.33
will be of type float
in C#
and so on…int example:
data-type variable-name = variable-value;
int valueForA = 1200;
string example:
string myName = "Abhishek Luv";
bool example:
bool IsAvailable = true;
These are the basic arithmetic operators used in C#. Let’s look at some sample code.
static void Main()
{
int a = 10; int b = 20;
int sum;
// here we have used + operator for addition
sum = a + b; Console.WriteLine(sum);
}
We can use the same + operator to join two string values in C#.
static void Main()
{
string firstName = "Abhishek";
string lastName = " Luv";
// here we have used + operator for joining two string values
string fullName = firstName + lastName;
Console.WriteLine(fullName);
}
static void Main()
{
PlayingWithIf(20, 20);
}
static void PlayingWithIf(int x, int y)
{
if (x < y)
{
Console.WriteLine("x is less than y");
}
else if (x > y)
{
Console.WriteLine("x is greater than y");
}
else if (x == y)
{
Console.WriteLine("x is equal to y");
}
else
{
Console.WriteLine("invalid numbers");
}
}
In this example, we have a static void method called PlayingWithIf
which uses If Else If
statements with relational operators to print the desired output on the console screen. Don’t worry about what is static
and void
. We’ll come to that.
if(x < y) // if x is less than y then print "x is less than y"
else if(x > y) // if x is greater than y then print "x is greater than y"
else if(x == y) // if x is equal to y then print "x is equal to y"
else....
Notice that the PlayingWithIf
method takes in two arguments
i.e. whenever the PlayingWithIf
method is called we will have to provide two arguments or two values to the method.
static void Main()
{
PlayingWithIf(20, 20);
}
Here, we are calling the PlayingWithIf
method in our Main method with two arguments (20,20).
It’s always better to write a standalone method than stuffing the entire Main method with code.
A For loop statement is used for looping to perform actions based on the initial value and the test condition declared within a For loop.
For example Lets print numbers 10 to 0 using a for loop
static void Main()
{
for (int i = 10; i >= 0; i--)
{
Console.WriteLine(i);
}
}
In this example, we have declared an initial variable int i
with a value of 10
i.e. we are saying the For loop to set the initial value equal to 10.
i >= 0;
is the condition defined within the loop. This instructs the for loop to keep running the loop until the value i
is greater than or equal to 0.
i--
is the action to performed by the for loop for us.
As the loop runs with the help of i--
the value i
is decremented by -1 till the condition i >= 0
is met.
let’s look at the same example using a while loop in C#.
static void Main()
{
int i = 10; // initial variables declared
while (i >= 0) // test condition
{
Console.WriteLine(i);
i--; // action to be performed every time the loop runs
}
}
In a While loop, we declare the initial variable and value outside the loop.
While loop starts with a test condition and then the body of the loop within curly braces {}.
Here, the condition is to keep running the loop until the value of i is greater than or equal to zero.
And the action to be performed in within the while loop i.e. i--
.
Do..While loop is very similar to the While loop but with only two differences.
First Difference
In a Do..While loop the statements inside the body of the loop are executed once before any conditions are met or the actions are performed by the loop.
For example:
static void Main()
{
int i = 0;
do
{
Console.WriteLine(i);
i++;
} while (i <= 10);
}
Second Difference
Here, an initial value of i
is 0. In a Do..While loop the condition is checked at the end of the loop and in a While loop at the beginning of the loop
And the actions i++
is performed within the Do loop.
Example:
static void Main()
{
CheckNumbers(10, 20);
}
static void CheckNumbers(int x, int y)
{
if (x >= 0 && y >= 0)
{
Console.WriteLine("both numbers positive");
}
else if (x >= 0 || y >= 0)
{
Console.WriteLine("at least one num is positive");
}
else
{
Console.WriteLine("both are negative");
}
}
In the above code example, we have a method called CheckNumbers
.
The CheckNumbers
method takes in two arguments to check whether the numbers are positive or negative.
if (x >= 0 && y >= 0) // 10 >= 0 && 20 >= 0 // Result will be : both numbers are positive
Condition 10 >= 0
will return true and 20 >= 0
will also return true. So, true * true is true i.e. both numbers are positive.
Similarly OR ||
is used to check at least one positive number and the else
block executes when both the numbers are negative.
Switch case statements are very similar to If statements. Switch case can be used with any data type.
In a switch case statement, we provide a value which it uses to compare with the cases defined within the switch statement and display appropriate results on the console.
Note: Case declared within a switch case statement has to be constant
static void Main(string[] args)
{
PlayingWithSwitchCase("three");
}
static void PlayingWithSwitchCase(string j)
{
switch (j)
{
case "two":
Console.WriteLine("you entered four in words");
break;
case "three":
Console.WriteLine("you entered three in words");
break;
default:
Console.WriteLine("error");
break;
}
}
Arrays are used to store a collection of data of similar data types.
For example: If you want to declared 5 int variable how will you do it?
Like this right?
int a = 1;
int b = 2;
int c = 3;
int d = 4;
int e = 5;
Now, this is a lot of code. It’s better to use arrays to store 5 int values than using 5 different int variables.
Declaring Arrays
int[] arrayName = new int[sizeofthearray];
int[] intArray = new int[5];
Now int[5] means that this array can hold 5 values. Values or data within an array can be accessed using an index.
For example:
intArray[0], intArray[1] and so on...
Note: Index of an array starts from value 0. i.e. If you want to access the 5th value then you need to use intArray[4]
Looping over an array to display data using For Looping
for (int i = 0; i < intArray.Length; i++)
{
Console.WriteLine(intArray[i]);
}
The for loop uses the intArray.Length
of the array to define a test condition for the loop.
Looping over an array using ForEach loop
foreach (int tempArrayValue in intArray)
{
Console.WriteLine(tempArrayValue);
}
The ForEach loop uses a tempArrayValue
variable to hold the array values one by one and display it on the console screen.
Anything within double quotes "Abhishek Luv"
in C# is a string.
Declaring a string
string myName = "Abhishek Luv";
Declaring an array of string
string[] allListOfName = { "Abhishek", "Abhijeet","Akshay" };
Commonly used String functions
Looping over an array of string using foreach loop
foreach (string names in allListOfName)
{
Console.WriteLine(names);
}
Day 1 over
Stay Tuned.
Take Care!
Abhishek Luv
by Abhishek Luv, Sunday, February 9, 2020 at 9:36 PM
Read moreby Abhishek Luv, Sunday, January 26, 2020 at 11:33 PM
Read moreby Abhishek Luv, Saturday, January 5, 2019 at 6:30 PM
Read more