Presentation is loading. Please wait.

Presentation is loading. Please wait.

C# Part 1 Intro to C#. Background Designed to be simple, modern, general- purpose, OO, programming language Strong type checking, array bounds checking,

Similar presentations


Presentation on theme: "C# Part 1 Intro to C#. Background Designed to be simple, modern, general- purpose, OO, programming language Strong type checking, array bounds checking,"— Presentation transcript:

1 C# Part 1 Intro to C#

2 Background Designed to be simple, modern, general- purpose, OO, programming language Strong type checking, array bounds checking, detection of attempts to use uninitialized variables, and automatic garbage collection –This doesn’t mean you still can’t write bad code! Garbage collection! Wasn’t made to compete with C or Assembly Event driven, just like VB

3 Background cont. Standards –ECMA-334 –ISO/IEC 23270

4 History Anders Hejlsberg started to work on “Cool” (C- like Object Oriented Language) Microsoft decided due to possible trademark concerns to change the name to C# C# has gone through 3 versions

5 Programming Very basic program (See p1.cs)

6 Compiling Click on Start –All Programs –Microsoft Visual Studio 2008 –Tools –Visual Studio 2008 Command Prompt (Later we will use Visual Studio to compile) cd to the directory where you are storing the source files (You can also use the build script) csc source.cs

7 using System; –Comparable to using namespace std; in C++ class FirstProgram –You can name the class whatever you want, unlike Java. static void Main() –Very similar to C++’s int main() –You MUST use a capital M in “Main” Console.WriteLine() –Allows you to output data to the console –(Very exciting I know)

8 Class Example (This is just to show you what you need to do to compile) Write a simple hello world program Input: None Output: “Hello, world”

9 Commenting Comments should be plentiful! 2 types –Single line using // –Multi line using /* */

10 Built in data types All the usual suspects are there…

11 Variables Declaring variables is just like in C++/Java datatype variable_name; The name can contain letters, digits, and the underscore character (_). The first character of the name must be a letter. The underscore is also a legal first character, but its use is not recommended at the beginning of a name. An underscore is often used with special commands, and it's sometimes hard to read. Case matters (that is, upper- and lowercase letters). C# is case-sensitive; thus, the names count and Count refer to two different variables. C# keywords can't be used as variable names.

12 If you feel the need to use a reserved keyword as a variable, use the @symbol Ie int @int = 5; This creates a constant called @int When would you want do this? Most likely, never.

13 Some modifiers: Static –Allows you to define a variable that you can access without having an instance of the class –HOWEVER….this is only a single value Public/private/protected –Controls who can access a variable in a class

14 Class Example Show how a static variable works vs non-static

15

16 Arithmetic Performing arithmetic operations is the same as in C++/Java var1 = var2 * var3; Example: int x,y; x = y + 5;

17 Outputting variables Use {#} to reference a variable in a string Ex. “Hello {0}”, where {0} would be the first variable passed into Console.WriteLine –Ex. Console.WriteLine(“Hello, {0}”, world); You can also concatenate using the plus symbol (See p2.cs)

18 Input To get input from the user use one the following methods: Console. –ReadLine This allows you to read a String from standard input –Read This allows you to read a single character from standard input –ReadKey This allows you detect any key with any modifiers *Note* - ReadLine returns a string!!!!!!!!!

19 Class Example Get 2 numbers from the user and perform basic arithmetic on them (+ - * /) Input 2 numbers Output: # + # = # # * # = # # / # = # # - # = #

20 Class Exercise Write a program that will ask for the users name and output it to the screen with a friendly message Example run: Please enter your name: Nathan Hello, Nathan, Big Brother is watching!

21 Control Structures Everything is the same as C++ Conditional –if (condition) statement –Ex. If (x > 5) { return 0; } While loop –while (expression) statement –Ex. while(x > 5) { Console.WriteLine(“while…”); } Do-while loop –do statement while (condition); do { Console.WriteLine(“Do, while”); } (x > 5);

22 Control Structures cont For loop –for (initialization; condition; increase) statement; –Ex. for (int x = 0; x < 5; x++) { Console.WriteLine(“for”); } Foreach loop –foreach (iterator in array) –Ex. foreach( int i in arr) { Console.WriteLine(“{0}”, i); }

23 Class Example Write a program using a for loop that accepts 5 numbers and tallies them up and then outputs the result

24 Class Exercise Write a program using a while loop that tallies up numbers from the user until they enter 0 Example: Enter a number: 5 Enter a number: 10 Enter a number: 0 Total = 15

25 Class Exercise Write a program using a while loop that tallies up numbers from the user until they enter 0 Example: Enter a number: 5 Enter a number: 10 Enter a number: 0 Total = 15

26 Command line parameters To use command line parameters (ie prog.exe param1) add string[] args as a parameter to the main function –Ex. static void Main(string[] args) This would be very useful for debugging your program (ie prog.exe -debug) (See p4.cs)

27 Functions Used to turn sections of code into a single line Functions are defined just like in C/C++ [modifier] return-type function-name(parameters) Some modifiers: extern, virtual, static Example: int doublenum(int num)… (See p5.cs)

28 Classes A class is an encapsulation of methods and variables To define a class: class ClassName {… Everything needs to be defined in a class Example: class cClass { }

29 The class that you define Main in, you should create a new instance of the class to access the methods/properties Ie class x = new class(); In C# you use dot notation to access methods Ie x.method(); (See p6.cs)

30 Class Example Write a program that has 2 classes: 1 with the Main function. The second class will be a small Math operations class Implement the function pow

31 Class Exercise Create 2 classes, 1 with the Main function. The second class with be a coin class, that will be able to calculate how much money you have in coins class cCoin { private intPennies = 0; public void setPennies(int n); public double calc(); //outputs the value in dollars: $0.23 Note: For time sake – just implement methods for pennies and nickels.

32 Credits http://en.wikipedia.org/wiki/C_Sharp_(programming_language) http://www.csharp-station.com/tutorials/lesson01.aspx http://www.cplusplus.com/doc/tutorial/control/ http://www.codeguru.com/csharp/sample_chapter/article.php/c11387/ http://stackoverflow.com/questions/91817/whats-the-use-meaning-of-the-character-in-variable-names-in-c


Download ppt "C# Part 1 Intro to C#. Background Designed to be simple, modern, general- purpose, OO, programming language Strong type checking, array bounds checking,"

Similar presentations


Ads by Google