1 / 121

C# - in Visual studio

C# - in Visual studio. Windows form application. II. Hello World. using System.Windows.Forms; using System.Drawing; class MyForm:Form{ public static void Main(){ Application.Run(new MyForm()); } protected override void OnPaint(PaintEventArgs e){

didier
Télécharger la présentation

C# - in Visual studio

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C# - in Visual studio

  2. Windows form application

  3. II. Hello World using System.Windows.Forms; using System.Drawing; class MyForm:Form{ public static void Main(){ Application.Run(new MyForm()); } protected override void OnPaint(PaintEventArgs e){ e.Graphics.DrawString("Hello World!", new Font("Arial", 35), Brushes.Blue, 10, 100); } }

  4. II. Hello World The source code in Figure 1‑1 displays the text "Hello World!" in a window. (The C# version of a command line hello-world application would be a one-liner). As you can see from the code, C# has a C-based syntax, but with objects like C++ or Java. Every function in C# is a method of a type. In this example, the MyForm class is defined to derive its functionality from the Form class (part of the .NET Framework Class Library). In addition it defines two new methods, Main() and OnPaint(). All C# (or .NET) applications must have a static method named Main() defined to be the entry point of the application.

  5. II. Hello World The static Main() method can be defined in any class in the application, so long as its name is “Main” and it is declared to be static. The OnPaint() method is an override of a virtual method on the Form class. It is called when the window needs to paint itself. This sample uses this method to draw the text "Hello World!". Most of the code in Figure 1‑1 will be covered in detail throughout this text. Don’t worry too much about the parts that don’t make sense right now. However, I would take the time to look the code over, perhaps build and run it, and get a feel for the sample.

  6. Basics of C#

  7. Array

  8. using System; class Array { public static void Main() { int[] myInts = { 5, 10, 15 }; bool[][] myBools = new bool[2][]; myBools[0] = new bool[2]; myBools[1] = new bool[1]; double[,] myDoubles = new double[2, 2]; string[] myStrings = new string[3]; Console.WriteLine("myInts[0]: {0}, myInts[1]: {1}, myInts[2]: {2}", myInts[0], myInts[1], myInts[2]); myBools[0][0] = true; myBools[0][1] = false; myBools[1][0] = true; Console.WriteLine("myBools[0][0]: {0}, myBools[1][0]: {1}", myBools[0][0], myBools[1][0]); myDoubles[0, 0] = 3.147; myDoubles[0, 1] = 7.157; myDoubles[1, 1] = 2.117; myDoubles[1, 0] = 56.00138917; Console.WriteLine("myDoubles[0, 0]: {0}, myDoubles[1, 0]: {1}",myDoubles[0, 0], myDoubles[1, 0]); myStrings[0] = "Joe"; myStrings[1] = "Matt"; myStrings[2] = "Robert"; Console.WriteLine("myStrings[0]: {0}, myStrings[1]: {1}, myStrings[2]: {2}", myStrings[0], myStrings[1], myStrings[2]); } }

  9. Binary Opreator

  10. using System; class Binary { public static void Main() { int x, y, result; float floatResult; x = 7; y = 5; result = x + y; Console.WriteLine("x+y: {0}", result); result = x - y; Console.WriteLine("x-y: {0}", result); result = x * y; Console.WriteLine("x*y: {0}", result); result = x / y; Console.WriteLine("x/y: {0}", result); floatResult = (float)x / (float)y; Console.WriteLine("x/y: {0}", floatResult); result = x % y; Console.WriteLine("x%y: {0}", result); result += x; Console.WriteLine("result+=x: {0}", result); } }

  11. Boolean opearator

  12. using System; class Booleans { public static void Main() { bool content = true; boolnoContent = false; Console.WriteLine("It is {0} that C# Stationprovides C# programming language content.", content); Console.WriteLine("The statement above is not {0}.", noContent); } }

  13. Unary opearetors

  14. using System; class Unary { public static void Main() { int unary = 0; int preIncrement; int preDecrement; int postIncrement; int postDecrement; int positive; int negative; sbyte bitNot; bool logNot; preIncrement = ++unary; Console.WriteLine("Pre-Increment: {0}", preIncrement); preDecrement = --unary; Console.WriteLine("Pre-Decrement: {0}", preDecrement); postDecrement = unary--; Console.WriteLine("Post-Decrement: {0}", postDecrement);

  15. postIncrement = unary++; Console.WriteLine("Post-Increment: {0}", postIncrement); Console.WriteLine("Final Value of Unary: {0}", unary); positive = -postIncrement; Console.WriteLine("Positive: {0}", positive); negative = +postIncrement; Console.WriteLine("Negative: {0}", negative); bitNot = 0; bitNot = (sbyte)(~bitNot); Console.WriteLine("Bitwise Not: {0}", bitNot); logNot = false; logNot = !logNot; Console.WriteLine("Logical Not: {0}", logNot); } }

  16. IF

  17. using System; class IfSelect { public static void Main() { string myInput; intmyInt; Console.Write("Please enter a number: "); myInput = Console.ReadLine(); myInt = Int32.Parse(myInput);// string to int if (myInt > 0) { Console.WriteLine("Your number {0} is greater than zero.",myInt); } if (myInt < 0) Console.WriteLine("Your number is less than zero.",myInt); if (myInt != 0) { Console.WriteLine("Your number {0} is not equal to zero.",myInt); } else { Console.WriteLine("Your number {0} is equal to zero.",myInt); } if (myInt < 0 || myInt == 0) { Console.WriteLine("Your number {0} is less than or equal to zero.",myInt); } else if (myInt > 0 && myInt <= 10) { Console.WriteLine("Your number {0} is between 1 and 10.",myInt); } else if (myInt >10 && myInt <= 20) { Console.WriteLine("Your number {0} is between 11 and 20.",myInt); } else if (myInt > 20 && myInt <= 30) { Console.WriteLine("Your number {0} is between 21 and 30.",myInt); } else { Console.WriteLine("Your number {0} is greater than 30.",myInt); } } }

  18. switch

  19. using System; class SwitchSelection { public static void Main() { string myInput; int myInt; begin: Console.Write("Please enter a number between 1 and 3: "); myInput = Console.ReadLine(); myInt = Int32.Parse(myInput); // Switch with integer type switch (myInt) { case 1: Console.WriteLine("Your number is {0}.", myInt); break; case 2: Console.WriteLine("Your number is {0}.", myInt); break; case 3: Console.WriteLine("Your number is {0}.", myInt); break; default: Console.WriteLine("Your number {0} is not between 1 and 3.", myInt); break; }

  20. decide: Console.Write("Type\"continue\"to go on or\"quit\"to stop: "); myInput = Console.ReadLine(); // switch with string type switch (myInput) { case "continue": goto begin; case "quit": Console.WriteLine("bye."); break; defualt: Console.WriteLine("Your input {0} is incorrect.", myInput); goto decide; } } }

  21. Loop Do while foreach

  22. using System; class ForEachLoop { public static void Main() { string[] names = {"Cheryl","Joe","Matt","Robert"}; foreach (string person in names) { Console.WriteLine("{0}",person); } } }

  23. For loop using System; class ForLoop { public static void Main() { for (int i=0; i < 20; i++) { if (i == 10) break; if (i % 2 ==0) continue; Console.Write("{0}",i); } Console.WriteLine(); } }

  24. While loop using System; class WhileLoop { public static void Main() { intmyInt = 0; while (myInt < 10) { Console.Write("{0}",myInt); myInt++; } Console.WriteLine(); } }

  25. Methods

  26. using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication3 { class myclass { public static int multiply(int x, int y) { int res = x * y; Console.WriteLine("Multiplication = " + res); return res; } public void inc(ref int x) { x = x + 1; } public void equal(int y) { y = y + 1; } }

  27. class Program { public int add(int x, int y) { return(x+y); } static void Main(string[] args) { int x = 90; int y = 90; // for static method //Console.WriteLine(" output = " + myclass.multiply(67,9887)); Program p = new Program(); int ans = p.add(56,89); Console.WriteLine("addition ==" + ans); // call by ref Console.WriteLine("X befor fuction call " + x); myc.inc(ref x); Console.WriteLine("X after fuction call " + x); // call by value Console.WriteLine("y befor fuction call " +y); myc.equal(y); Console.WriteLine("y after fuction call " + y); } } }

  28. Name Sapce

  29. namespace csharp_station.tutorial { class myExample { public static void myPrint() { Console.WriteLine("This is a member of csharp_station.tutorial.myExample."); } } }

  30. // Namespace Declaration using System; using csTut = csharp_station.tutorial.myExample; // alias // Program start class class AliasDirective { // Main begins program execution public static void Main() { // Call namespace member csTut.myPrint(); myPrint(); } // Potentially ambiguous method static void myPrint() { Console.WriteLine("Not a member of csharp_station.tutorial.myExample."); } }

  31. Nested Name space

  32. using System; namespace csharp_station { // nested namespace namespace tutorial { class myExample1 { public static void myPrint1() { Console.WriteLine("First Example of calling another namespace member."); } } } // program start class class NamespaceCalling { // Main begins program execution public static void Main() { // Write to console tutorial.myExample1.myPrint1(); tutorial.myExample2.myPrint2(); } } }

  33. namespace csharp_station.tutorial { class myExample2 { public static void myPrint2() { Console.WriteLine("Second Example of calling another namespace member."); } } }

  34. Class

  35. // Namespace Declaration using System; // helper class class OutputClass { string myString; // Constructor public OutputClass(string inputString) { myString = inputString; } // Instance Method public void printString() { Console.WriteLine("{0}", myString); } // Destructor ~OutputClass() { // Some resource cleanup routines } } // Program start class class ExampleClass { // Main begins program execution. public static void Main() { // Instance of OutputClass OutputClassoutCl = new OutputClass("This is printed by the output class."); // Call Output class' method outCl.printString(); } }

  36. Inheritance

  37. using System; public class ParentClass { public ParentClass() { Console.WriteLine("Parent Constructor."); } public void print() { Console.WriteLine("I'm a Parent Class."); } } public class ChildClass : ParentClass { public ChildClass() { Console.WriteLine("Child Constructor."); } public static void Main() { ChildClass child = new ChildClass(); child.print(); }

More Related