.


:




:

































 

 

 

 


-




( , , , ). - .

.NET Framework Console, , -, .

Console :

BackgroundColor ( ConsoleColor).

CapsLock true, CapsLock.

CursorLeft , .

CursorSize .

CursorTop , .

CursorVisible .

Error ( cerr ++).

ForegroundColor ( ConsoleColor).

In .

LargestWindowHeight , .

LargestWindowWidth , .

NumberLock true, NUM LOCK/

Out .

Title .

WindowHeight .

WindowLeft , .

WindowTop , .

WindowWidth .

Console, -:

Beep .

Clear .

OpenStandardError .

OpenStandardInput .

OpenStandardOutput .

Read .

ReadKey ( ConsoleKeyInfo).

ReadLine .

ResetColor .

SetBufferSize .

SetCursorPosition .

SetError System.IO.TextWriter, , Error.

SetIn System.IO.TextReader, , In.

SetOut System.IO.TextWriter, , Out.

SetWindowPosition .

SetWindowSize .

Write .

WriteLine Write, , \n ( ).

Console.Title = Console;

Console.BackgroundColor = ConsoleColor.White;

Console.ForegroundColor = ConsoleColor.DarkGreen;

:

Console.Title = " Console";

Console.BackgroundColor = ConsoleColor.White;

Console.ForegroundColor = ConsoleColor.DarkGreen;

Console.SetWindowSize(15, 8);

Console.SetBufferSize(15, 8);

Console.WriteLine("Some otuput data(green color)")

Console.ResetColor();

Console.write("Enter your name: ");

string name = Console.ReadLine();

Console.WriteLine("Hello, " + name + "!");

 

Math

System.Math , :

Abs() - ;

Cos() - ;

Sin() - ;

Floor() - , ;

Max() - ;

Min() - ;

Pow() - ;

Round() - .

Math:

double result = Math.Max(1, Math.Min(100, number)); // ensure number in range 1..100

double afterDot = number - Math.Floor(number); //

6

C#, ++ , . , . , ( ) .

, , , , studentID, firstName, lastName, group, .. .

, , . , . , new . (, , false, null).

:

[][] Class < >

{

[][] < 1>;

[][] < 2>;

 

[][] <1()>

{

}

[][] <2()>

{

}

}

, ++, . -

. , , .

.

:

public class NoteBook {

int counter = 0;

string[] notes = new string[10];

public void addNote(string note) {

if (counter - 1 < notes.Length) {

notes[counter++] = note;

}

}

}

. , , (public) (internal). . , . # . ( ) . , . CLR , .

(private) (public):

private

protected ( )

internal

protected internal

public

, , . , , , , .

. - , .

, # .

# , . CLR , .

. : , , , , , , , , .

. , . :

, , , .

static , , .

readonly , ( ).

CLR (read/write) (readonly) . . , . , , , . , , , , . , , . .

:

public class Librarian {

readonly NoteBook noteBook = new NoteBook();

}

++ , . # . .

- , ++. ++ , # . public:, private: .

# , , ++.

# , , , ( ), , , :

[][] _ < >([])

{

//

}

. , , , . - .

, NoteBook, Librarian. Notebook , Librarian .

public class NoteBook {

int counter = 0; //-

string[] notes = new string[10]; //

 

public int getNoteCount() {

return counter;

}

 

public string getNote(int index) {

return notes[index];

}

 

public string getLastNote() {

if (counter == 0) {

return "";

}

return notes[counter - 1];

}

 

public void addNote(string note) {

if (counter - 1 < notes.Length) {

notes[counter++] = note;

}

}

}

 

public class Librarian { // , NoteBook

const string ADD_COMMAND = "add "; // NoteBook

const string GET_COMMAND = "get "; //

const string SIZE_COMMAND = "size"; // -

 

readonly NoteBook noteBook = new NoteBook();

 

public void performOperation(string command, string argument) {

switch(command) {

case ADD_COMMAND:

addNote(argument);

break;

case GET_COMMAND:

getNote(argument);

break;

case SIZE_COMMAND:

size();

break;

}

}

 

void addNote(string note) {

noteBook.addNote(note);

Console.WriteLine("Note \"" + noteBook.getLastNote() + "\" was added!");

}

 

void getNote(string argument) {

int index = Int32.Parse(argument);

Console.WriteLine("Note " + index + ": " + noteBook.getNote(index));

}

 

void size() {

Console.WriteLine("Current notebook size = " + noteBook.getNoteCount());

}

}

public static void Main(string[] args) {

Librarian librarian = new Librarian();

string inputCommand;

do {

inputCommand = Console.ReadLine(); // - 4 , , "get 2" "add new note"

string command = inputCommand.Substring(0, 4);

string commandArg = inputCommand.Substring(4);

librarian.performOperation(command, commandArg);

} while (inputCommand!= "quit");

}

( ):

size

Current notebook size = 0

add my new note

Note "my new note" was added!

add and one more

Note "and one more" was added!

add third note

Note "third note" was added!

add and last one

Note "and last one" was added!

size

Current notebook size = 4

get 1

Note 1: and one more

quit

7

, . C# 3 . , . .

. ++ ( ), , , false , null . . .

, - .

, , . . - static.

, ( ) public, , , ( void), , , . .

: , , .

This

this , (.. ). : , , - this; this . ( ), , .

:

class Music {

private static string STUB_ARIST;

private static string STUB_DURATION;

private static string STUB_NAME;

 

private string artist;

private string duration;

private string name;

 

static Music() {

STUB_ARIST = "Unknown artist";

STUB_DURATION = "--:--";

STUB_NAME = "Uknwown name";

}

 

public Music() {

artist = STUB_ARIST;

duration = STUB_DURATION;

name = STUB_NAME;

 

}

 

public Music(string duration, string name):this(STUB_ARIST, duration, name) {

}

 

public Music(string artist, string duration, string name) {

this.artist = artist;

this.duration = duration;

this.name = name;

}

 

public void setArtist(string artist) {

this.artist = artist;

}

 

public void setDuration(int seconds) {

this.duration = seconds / 60 + ":" + seconds % 60;

}

 

public void setName(string name) {

this.name = name;

}

 

public void printSong() {

Console.WriteLine(artist + " - " + name + "\t" + duration);

}

}

ref out

, (, , ). , ref out. :

class Storage {

private int value;

public void setValue(int value) {

this.value = value;

}

public int getValue() {

return value;

}

}

 

public static void changeVariables(int first, int[] second, int[] input3, Storage storage) {

first++;

second[0]++;

input3 = new int[] {-1};

storage.setValue(storage.getValue() + 1);

}

 

int input = 5;

int[] input2 = new int[] {10, 11}, input3 = new int[] {3, 33};

Storage storage = new Storage();

storage.setValue(-3);

changeVariables(input, input2, input3, storage);

//input = 5, input2 = [11, 11], input3 = [3, 33], storage.value = -2

ref , . , , . , ref, , .

:

public static void changeVariables(ref int first, ref int[] second, ref int[] input3, ref Storage storage) {

first++;

second[0]++;

input3 = new int[] {-1};

storage.setValue(storage.getValue() + 1);

}

int input = 5;

int[] input2 = new int[] {10, 11}, input3 = new int[] {3, 33};

Storage storage = new Storage();

storage.setValue(-3);

changeVariables(ref input, ref input2, ref input3, ref storage);

//input = 6, input2 = [11, 11], input3 = [-1], storage.value = -2

, out, . ref , , ( ).

# , CLR . partial #, .

partial . , . dll. #; .

:

partial class Rectangle {

int width;

int height;

 

public int getWidth() {

return width;

}

 

public void setWidth(int width) {

this.width = width;

}

 

public void setHeight(int height) {

this.height = height;

}

 

public int getHeight() {

return height;

}

}

 

partial class Rectangle {

int color;

 

public void setColor(int color) {

this.color = color;

}

 

public int getColor() {

return color;

}

}

public static void Main(string[] args) {

Rectangle rectangle = new Rectangle();

rectangle.setColor(0xff0000);//RRGGBB red.

rectangle.setWidth(100);

rectangle.setHeight(30);

Console.ReadKey();

}

8

, . , , , . , , System.Object ( object).

C# . , C# , . , .

C# :

class :

{

// , ,

}

, :

class : , 1, 2

{

// , ,

}

, . , . . . , System.Object, . , System.Object , . .

. - , base().

public (): base()

{

// , ,

}

:

interface Printable {

void printSelf();

}

class Rectangle {

int width;

int height;

public Rectangle() {

 

}

public Rectangle(int width, int height) {

this.width = width;

this.height = height;

}

public int Width {

get{ return width;}

set{ width = value;}

}

public int Height {

get { return height; }

set { height = value; }

}

}

class ColoredRectangle: Rectangle, Printable {

string color;

public ColoredRectangle() {

 

}

public ColoredRectangle(int width, int height): base(width, height) {

 

}

public ColoredRectangle(int width, int height, string color): base(width, height) {

this.color = color;

}

public string Color {

get { return color;}

set { color = value;}

}

public void printSelf() {

Console.WriteLine("Colored rectangle {0}x{1} ({2})", Width, Height, color);

}

}

public static void Main(string[] args) {

ColoredRectangle rectangle = new ColoredRectangle(10, 3, "red");

rectangle.printSelf(); //Colored rectangle 10x3 (red)

Console.ReadKey();

}





:


: 2016-12-03; !; : 1093 |


:

:

, .
==> ...

1515 - | 1344 -


© 2015-2024 lektsii.org - -

: 0.206 .