📜 ⬆️ ⬇️

Visual C # for beginners. Lecture 5. Type conversion. Enumerations, structures, arrays

Hello!


Finally, I recorded the fifth lecture of Visual C # for beginners. In this lecture, I will tell you about variable type conversion. Then you will learn about enums, structures, and arrays. Of course, you also want to apologize for so long absence of lectures. This is due to the fact that I organized and spent the days of Microsoft TechDays in my university, and there was no time to work in this direction. Also, this lecture is most likely the last one this year! I received a lot of work solved, for which I am very grateful to everyone who sent and tried to solve. The correct answers, who could not send me an email, are in the post below. Also, the source code that was used to record the lecture can be found under the video.

Links to previous lectures

Lecture 1. Introduction
Lecture 2. Hello, World! and familiarity with Visual C # Express 2010
Lecture 3. Variables and Expressions
Lecture 4. Conditions and cycles

Answers to previous homework:

1. If there are two integers stored in the variables var1 and var2, which boolean check needs to be performed to find out if one or the other (but not both) is greater than 10?
')
(var1> 10) ^ (var2> 10)

2. Write an application that receives two numbers from the user and displays them on the screen, but rejects the options when both numbers are greater than 10, and in this case offers to enter two other numbers.

static void Main( string [] args)
{
double var1, var2;
Console .Write( "First: " ); Double.TryParse( Console .ReadLine(), out var1);
Console .Write( "Second: " ); Double.TryParse( Console .ReadLine(), out var2);

while (var1 > 10 && var2 > 10)
{
Console .WriteLine( "More than 10. Again." );
Console .Write( "First: " ); Double.TryParse( Console .ReadLine(), out var1);
Console .Write( "Second: " ); Double.TryParse( Console .ReadLine(), out var2);
}

Console .WriteLine( "First: {0}, second: {1}. Congrats!" , var1, var2);

Console .ReadKey();
}


* This source code was highlighted with Source Code Highlighter .


3. What is wrong with the following code (try to solve this task without using Visual Studio)?
Instead of “if ((i% 2) = 0)” should be “if ((i% 2) == 0)”

Enjoy watching!



Source codes from lectures

Laboratory work №1. Type conversion
static void Main( string [] args)
{
short shortResult, shortVal = 4;
int integerVal = 67;
long longResult;
float floatVal = 10.5f;
double doubleResult, doubleVal = 99.999;
string stringResult, stringVal = "17" ;
bool boolVal = true ;

Console .WriteLine( "Variable Conversion Examples\n" );

doubleResult = floatVal*shortVal;
Console .WriteLine( "Implicit, -> double: {0} * {1} -> {2}" , floatVal, shortVal, doubleResult);

shortResult = ( short ) floatVal;
Console .WriteLine( "Explicit, -> short: {0} -> {1}" , floatVal, shortResult);

stringResult = Convert .ToString(boolVal) + Convert .ToString(doubleVal);
Console .WriteLine( "Explicit, -> string: \"{0}\" + \"{1}\" -> {2}" , boolVal, doubleVal, stringResult);

longResult = integerVal + Convert .ToInt64(stringVal);
Console .WriteLine( "Mixed, -> long: {0} + {1} -> {2}" , integerVal, stringVal, longResult);

Console .ReadKey();
}


* This source code was highlighted with Source Code Highlighter .


Laboratory work №2. Enumerations.
enum orientation : byte
{
north = 1,
south = 2,
east = 3,
west = 4
}

static void Main( string [] args)
{
byte directionByte;
string directionString;
orientation myDirection = orientation.north;
Console .WriteLine( "myDirection = {0}" , myDirection);
directionByte = ( byte ) myDirection;
directionString = Convert .ToString(myDirection);
Console .WriteLine( "byte equivalent = {0}" , directionByte);
Console .WriteLine( "string equivalent = {0}" , directionString);

Console .ReadKey();
}


* This source code was highlighted with Source Code Highlighter .


Laboratory work number 3. Structures.
enum orientation : byte
{
north = 1,
south = 2,
east = 3,
west = 4
}

struct route
{
public orientation direction;
public double distance;
}

static void Main( string [] args)
{
route myRoute;
int myDirection = -1;
double myDistance;
Console .WriteLine( "1) North\n2) South\n3) East\n4) West" );
do
{
Console .WriteLine( "Select a direction:" );
myDirection = Convert .ToInt32( Console .ReadLine());
} while ((myDirection < 1) || (myDirection > 4));
Console .WriteLine( "Input a distance:" );
myDistance = Convert .ToDouble( Console .ReadLine());
myRoute.direction = (orientation) myDirection;
myRoute.distance = myDistance;
Console .WriteLine( "myRoute specifies a direction of {0} and a distance of {1}" ,
myRoute.direction, myRoute.distance);

Console .ReadKey();
}


* This source code was highlighted with Source Code Highlighter .


Laboratory №4. Arrays
static void Main( string [] args)
{
string [] friendNames = { "Robert" , "Mike" , "Jeremy" };
Console .WriteLine( "Here are {0} of my friends:" , friendNames.Length);

foreach ( string friendName in friendNames)
{
Console .WriteLine(friendName);
}

Console .ReadKey();
}


* This source code was highlighted with Source Code Highlighter .


New homework

1. Which of the operations can not be performed implicitly:
a) int in short
b) short to int
c) bool in string
d) byte to float

2. Create a code based on the short type for listing a color containing 4 different colors. Can such an enumeration be based on byte?

3. Will the following code be compiled and why?
string [] blab = new string [5]
string [5] = 5th string ;

* This source code was highlighted with Source Code Highlighter .


PS Happy New Year, friends!
You can also subscribe to my Vimeo channel to be the first to know about the release of a new video.

Source: https://habr.com/ru/post/109885/


All Articles