.
Microsoft Visual C# 2013 Express -> -> -> -> Console Application ( ) -> . *.cs . .
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
}
}
}
, . Main, Hello, World! .
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
Console.ReadKey();
}
}
}
F5. , .
, , Program, () Main, , . Main , . Console.WriteLine("Hello, World!"); . , , , Console.ReadKey(); . .
. . , , .
- . . , -:
Sbyte | -128 127 | 8- |
Byte | 0 255 | 8- |
Char | U+0000 U+ffff | 16- Unicode |
Bool | true false | 1 * |
Short | -32768 32767 | 16- |
Ushort | 0 65535 | 16- |
Int | -2147483648 2147483647 | 32- |
uint | 0 4294967295 | 32- |
long | -9223372036854775808 9223372036854775807 | 64- |
ulong | 0 18446744073709551615 | 64- |
float | 1,5*10-45 3,4*1033 | 4 , 7 |
double | 5*10-324 1,7*10306 | 8 , 16 |
decimal | (-7,9 * 1028 7,9 * 1028) / (10028) | 16 , 28 |
|
|
. - , . bool : 0 - false, 1 - true, - , , - .
, , :
static void Main(string[] args)
{
int a; // a int
a = 5; // a 5
int b, c; //
bool d; // d bool
d = true; // d true ()
long e = 10; // ,
float f = 5.5f; // float, f.
char g = 'g'; // g 'g'
}
, , "Use of unassigned local variable [variableName]".
static void Main(string[] args)
{
int a;
Console.WriteLine(a); //
}
- . max Max . , .
, . a, b, c. . height, age . .
.
. . .
:
static void Main(string[] args)
{
int a = 35;
short b = 10;
a = b; // . int , short
b = a; // ,
}
, , ,
:
static void Main(string[] args)
{
int a = 35000;
short b = 10;
b = (short) a; // . short 32767, .
}
, . , .
, , const:
static void Main(string[] args)
{
const int months = 12; //
months = 13; //
}
. , .
static void Main(string[] args)
{
const int months = 12;
const int monthSalary = 1024;
int yearSalary = monthSalary * months;
}
:
static void Main(string[] args)
{
int yearSalary = 12 * 1024;
}
: :
|
|
static void Main(string[] args)
{
Console.WriteLine(100); // 100 100 , ,
Console.WriteLine("Hello!"); // Hello!
}
-, , .
var
C# 3.0 var, . .
static void Main(string[] args)
{
var number = 5; // number int
var text ="some text"; // text string
var number2 = 0.5; // number2 double
}
var -. , , , :
static void Main(string[] args)
{
var number = 5;
number = "some text"; // , number int
}
var LINQ ( )
static void Main(string[] args)
{
var query = from s in bdContext.Students select s;
}
var - var :
-
-
-
- , null
var C#, , ( ).
, , . . object string.
object . string Unicode.
string.
static void Main(string[] args)
{
string hello = "Hello!";
Console.WriteLine(hello);
}
- . .
, . . , , , .
. , ( ). , . .
: . , . . , . , + 2+3, 2 3. :
a + b | |
a - b | |
a / b | |
a * b | |
a % b |
. 9/5 1. , / float double. , 9 / 5f ( f , float) 1.8.
% . 9 % 5 4. % . 2. , 0, 1.
|
|
, , .
2+2*2=6
(2+2)*2=8
+ , . ( ) .
static void Main(string[] args)
{
string str1 = "Hello", str2 = "World";
Console.WriteLine(str1 + ", " + str2); // "Hello, World"
}