C# 5.0, one language to rule them all

Sunday, May 25, 2014

Download visual studio express 2013 for windows desktop here

Visual studio install, installs the .net framework, provides libraries to handle, database communication, sql, files, http, speech, dates and times, printing, xml, timers, build web applications, build desktop applications, build windows services, .net framework is the biggest library created in the history of humanity

Clr, the compiler is execution environment for application, it does memory management, it tracks all objects requested to do work, cleans up the objects, the garbage collector does this, clr is similar to a virtual machine, it ensures operating system and hardware independence, language independence

Fcl, framework class library, library of functionality to build great applications, contains thousand of classes to build applications, parse text, use regular expressions(there are two categories of people, those that know regex and those that have regular sex), communicate with sockets over the network, the bcl, base class library is a subset of the fcl, some areas are: asp.net, wcf, windows forms, wpf...

C#, create applications, services and reusable libraries, syntax similar to java, c++, javascript, here is a snippet of c# code

Open command prompt, navigate to documents folder, use notepad to create file helloworld.cs

Type in this simple hello world code, using references a class library that console class requires, main is the method invoked by the console app, it's static, the string literal will be written out to the console

Navigate to framework 4 directory and invoke csc.exe, this is the compiler

Run the compiler against the c# file, it has now been compiled

View there is now a hello executable in the documents folder

Run the executable by specifying its name and view the output

Replace string literal with datetime from system namespace and now to retrieve the current date and time, save the file

Recompile the file and invoke the executable, view current date time written out to the command prompt console window

Csc.exe, the c# command line compiler transforms c# code into microsoft intermediate language

File new project, windows console application

Type in this line of code to print out the string to the console window, build the project, view the location of the executable

Navigate to the location in the command prompt, open the directory, view the executable is located there

Run the executable, see it writes out hello rio 2016

Type in this line of code to grab the first item of args array passed into the main method

Concatenate the string with the variable using the plus sign in the console writeline invocation, save and build the project

Run the executable passing in a parameter, see the parameter rendered in output stream

Type in this code to write to the console and read from the console, save and build the project

Run the executable type in a name, hit enter, view the output stream in the console window

Grab the number from the console, convert it to an int, add this branching logic, with expression and less than operator to print out a conditional message, save and build the project

Run the program, type in these values, view the results in the console window

File new project, windows console application

Add a new class to the project

Add this code, a field definition of type list, create an instance, a method, takes in a parameter and adds it to the list

In the program class, in main method, create an instance of grades class, declare a variable and instantiate it using the new keyword, invoke the method on the variable, add a couple of numbers, use suffix f for floating point to inform the compiler that the int and double are to be treated as floating point numbers

Add a constructor and initialize the list in it

Create a new instance of grades class, invoke addgrade method, position cursor on line 19, right click and select run to cursor, view in locals window that 2 instances of grades have been created that hold the values

Assign grades variable to grades1, classes are reference types, indirection, they point to a location in memory, now both variables point to the same object and hold all 3 values, see locals window

Add this new class to the project

Add three public fields, in constructor initialize the low and high fields

Type in the calculatestatistics method in the grades class, calculate the average in the loop, grab the highest and lowest grade, return a statistics object to the caller

Print out the calculated statistics to the console

Run the program, view the results in console window

Add this public field to the grades class

Classes are reference types, variables store reference to an object, both grade variables point to the same object

Create a second instance of grades, view console window is empty

Default Success Warning Important Info Inverse
B S W I ? O