C# NOTES
AND TOTURIALS
Data Type
Data types are used everywhere in a programming language like
C#. Because it's a strongly typed language, you are required to inform the
compiler about which data types you wish to use every time you declare a
variable, as you will see in the chapter about variables. In this chapter we
will take a look at some of the most used data types and how they work.
bool is one of the simplest data types. It can contain only 2 values - false or true. The bool type is important to understand when using logical operators like the if statement.
int is short for integer, a data type for storing numbers without decimals. When working with numbers, int is the most commonly used data type. Integers have several data types within C#, depending on the size of the number they are supposed to store.
string is used for storing text, that is, a number of chars. In C#, strings are immutable, which means that strings are never changed after they have been created. When using methods which changes a string, the actual string is not changed - a new string is returned instead.
char is used for storing a single character.
float is one of the data types used to store numbers which may or may not contain decimals.
bool is one of the simplest data types. It can contain only 2 values - false or true. The bool type is important to understand when using logical operators like the if statement.
int is short for integer, a data type for storing numbers without decimals. When working with numbers, int is the most commonly used data type. Integers have several data types within C#, depending on the size of the number they are supposed to store.
string is used for storing text, that is, a number of chars. In C#, strings are immutable, which means that strings are never changed after they have been created. When using methods which changes a string, the actual string is not changed - a new string is returned instead.
char is used for storing a single character.
float is one of the data types used to store numbers which may or may not contain decimals.
A variable can be compared to
a storage room, and is essential for the programmer. In C#, a variable is
declared like this:
<data type> <name>;
An example could look like this:
string name;
That's the most basic version. Usually, you wish to assign a visibility to the variable, and perhaps assign a value to it at the same time. It can be done like this:
<visibility> <data type> <name> = <value>;
And with an example:
<data type> <name>;
An example could look like this:
string name;
That's the most basic version. Usually, you wish to assign a visibility to the variable, and perhaps assign a value to it at the same time. It can be done like this:
<visibility> <data type> <name> = <value>;
And with an example:
private string name = "John Doe";
Basic structure of c#
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, world!");
Console.ReadLine();
}
}
}
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string firstName = "John";
string lastName = "Doe";
Console.WriteLine("Name:
" + firstName + " " + lastName);
Console.WriteLine("Please
enter a new first name:");
firstName = Console.ReadLine();
Console.WriteLine("New name:
" + firstName + " " + lastName);
Console.ReadLine();
}
}
}
User
Imput concept c#
int number1, number2;
Console.WriteLine("Please enter a number:");
number1 = int.Parse(Console.ReadLine());
Console.WriteLine("Thank you. One more:");
number2 = int.Parse(Console.ReadLine());
Console.WriteLine("Adding the two numbers: " + (number1 + number2));
Console.ReadLine();
IF
STATEMENT
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int number;
Console.WriteLine("Please enter a number between 0 and 10:");
number = int.Parse(Console.ReadLine());
if(number > 10)
Console.WriteLine("Hey! The number should be 10 or less!");
else
if(number < 0)
Console.WriteLine("Hey! The number should be 0 or more!");
else
Console.WriteLine("Good job!");
Console.ReadLine();
}
}
}
SWITCH CASE
int number = 1;
switch(number)
{
case 0:
Console.WriteLine("The number is zero!");
break;
case 1:
Console.WriteLine("The number is one!");
break;
}
Console.WriteLine("Do you enjoy C# ? (yes/no/maybe)");
string input = Console.ReadLine();
switch(input.ToLower())
{
case "yes":
case "maybe":
Console.WriteLine("Great!");
break;
case "no":
Console.WriteLine("Too bad!");
break;
}
-----------------------
Console.WriteLine("Do you enjoy C# ? (yes/no/maybe)");
string input = Console.ReadLine();
switch(input.ToLower())
{
case "yes":
case "maybe":
Console.WriteLine("Great!");
break;
case "no":
Console.WriteLine("Too bad!");
break;
default:
Console.WriteLine("I'm sorry, I don't understand that!");
break;
}
While loop
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int number = 0;
while(number < 5)
{
Console.WriteLine(number);
number = number + 1;
}
Console.ReadLine();
}
}
}
The foreach loop
The last loop we will look
at, is the foreach loop. It operates on collections of items, for instance
arrays or other built-in list types. In our example we will use one of the
simple lists, called an ArrayList. It works much like an array, but don't
worry, we will look into it in a later chapter.
using System;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
ArrayList list = new ArrayList();
list.Add("John Doe");
list.Add("Jane Doe");
list.Add("Someone Else");
foreach(string name in list)
Console.WriteLine(name);
Console.ReadLine();
}
}
}
No comments:
Post a Comment