c# at f#


Published on

Published in: Education, Technology
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

c# at f#

  1. 1. C# and F#<br />Programming Language<br />
  2. 2. C SHARP<br />It was developed by Microsoft within its .NET initiative and later approved as a standard by Ecma (ECMA-334) and ISO (ISO/IEC 23270). <br />C# is one of the programming languages designed for the Common Language Infrastructure.<br />"C sharp" was inspired by musical notation where a sharp indicates that the written note should be made a semitone higher in pitch.<br />C#'s principal designer and lead architect at Microsoft is Anders Hejlsberg, who was previously involved with the design of Turbo Pascal, Embarcadero Delphi.<br />
  3. 3. Design Goals of C#<br />C# language is intended to be a simple, modern, general-purpose, object-oriented programming language.<br />The language, and implementations thereof, should provide support for software engineering principles such as strong type checking, array bounds checking, detection of attempts to use uninitialized variables, and automatic garbage collection.<br />The language is intended for use in developing software components suitable for deployment in distributed environments.<br />Source code portability is very important, as is programmer portability, especially for those programmers already familiar with C and C++.<br />
  4. 4. Design Goals of C#<br />Support for internationalization is very important.<br />C# is intended to be suitable for writing applications for both hosted and embedded systems, ranging from the very large that use sophisticated operating systems, down to the very small having dedicated functions.<br />Although C# applications are intended to be economical with regard to memory and processing power requirements, the language was not intended to compete directly on performance and size with C or assembly language.<br />
  5. 5. Versions of C#<br />
  6. 6. Distinguishing Features of C#<br />It has no global variables or functions. All methods and members must be declared within classes. Static members of public classes can substitute for global variables and functions.<br />Local variables cannot shadow variables of the enclosing block, unlike C and C++. Variable shadowing is often considered confusing by C++ texts.<br />C# supports a strict Boolean data type.<br />In C#, memory address pointers can only be used within blocks specifically marked as unsafe, and programs with unsafe code need appropriate permissions to run. <br />Managed memory cannot be explicitly freed.<br />
  7. 7. Distinguishing Features of C#<br />Multiple inheritance is not supported, although a class can implement any number of interfaces.<br />C# is more type safe than C++. <br />C# currently (as of version 4.0) has 77 reserved words.<br />
  8. 8. Categories of data types<br />Value types<br /><ul><li>Value types are plain aggregations of data. Instances of value types do not have referential identity nor a referential comparison semantics - equality and inequality comparisons for value types compare the actual data values within the instances, unless the corresponding operators are overloaded.
  9. 9. Examples of value types are all primitive types, such as int (a signed 32-bit integer), float (a 32-bit IEEE floating-point number), char (a 16-bit Unicode code unit), and System.DateTime (identifies a specific point in time with nanosecond precision).</li></li></ul><li>Categories of data types<br />Reference types<br /><ul><li>reference types have the notion of referential identity - each instance of a reference type is inherently distinct from every other instance, even if the data within both instances is the same.
  10. 10. Examples of reference types are object (the ultimate base class for all other C# classes), System.String (a string of Unicode characters), and System.Array (a base class for all C# arrays).</li></li></ul><li>Code comments<br />C# utilizes a double forward slash (//) to indicate the rest of the line is a comment. This is inherited from C++.<br />public class Foo<br />{<br /> // a comment<br /> public static void Bar(intfirstParam) {} // also a comment<br />}<br />Multi-line comments can be indicated by a starting forward slash/asterisk (/*) and ending asterisk/forward slash (*/). This is inherited from standard C.<br />public class Foo<br />{<br />/* A Multi-Line<br /> comment */<br /> public static void Bar(intfirstParam) {}<br />}<br />
  11. 11. Preprocessor<br />C# features "preprocessor directives" (though it does not have an actual preprocessor) based on the C preprocessor that allow programmers to define symbols but not macros. Conditionals such as #if, #endif, and #else are also provided. Directives such as #region give hints to editors for code folding.<br />public class Foo<br />{<br />#region Procedures<br /> public void IntBar(intfirstParam) {}<br /> public void StrBar(string firstParam) {}<br /> public void BoolBar(bool firstParam) {}<br /> #endregion<br /> #region Constructors<br /> public Foo() {}<br /> public Foo(intfirstParam) {}<br /> #endregion<br />}<br />
  12. 12. "Hello world" example<br />using System;<br />class Program<br />{<br /> static void Main()<br /> {<br />Console.WriteLine("Hello world!");<br /> }<br />}<br />
  13. 13. using System;<br /><ul><li>The using statement allows the programmer to state all candidate prefixes to use during compilation instead of always using full type names.</li></ul>class Program<br /><ul><li>Above is a class definition. Everything between the following pair of braces describes Program.</li></ul>static void Main()<br /><ul><li>This declares the class member method where the program begins execution. The void keyword declares that Main has no return value.</li></ul>Console.WriteLine("Hello world!");<br /><ul><li>The program calls the Console method WriteLine, which displays on the console a line with the argument, the string "Hello world!".</li></li></ul><li>Comparison of C Sharp and Java<br />Both languages are considered "curly brace" languages in the C/C++ family. Overall the syntaxes of the languages are very similar.<br />The syntax at the statement and expression level is almost identical with obvious inspiration from the C/C++ tradition.<br />Java is explicit about extending classes and implementing interfaces, while C# infers this from the kind of types a new class/interface derives from.<br />C# supports more features than Java which to some extent is also evident in the syntax which specifies more keywords and more grammar rules than Java.<br />
  14. 14. Simple/primitive types<br />
  15. 15. Advanced numeric types<br />
  16. 16. Lifted (nullable) types<br />
  17. 17. Special feature keywords<br />
  18. 18. Special feature keywords<br />
  19. 19. C Sharp Identifier<br />An identifier can:<br /><ul><li>start with a "_".
  20. 20. contain both upper case and lower case Unicode letters. Case is significant.</li></ul>An identifier cannot:<br /><ul><li>start with a numeral.
  21. 21. start with a symbol, unless it is a keyword (check Keywords).
  22. 22. have more than 511 chars.</li></li></ul><li>C# keywords, reserved words<br />
  23. 23. C Sharp Literals<br />
  24. 24. C Sharp Literals<br />
  25. 25. Variables<br />Variables are identifiers associated with values. They are declared by writing the variable's type and name, and are optionally initialized in the same statement by assigning a value.<br />Declare<br />intMyInt; // Declaring an uninitialized variable called 'MyInt', of type 'int'<br />Initialize<br />intMyInt; // Declaring an uninitialized variable<br />MyInt = 35; // Initializing the variable<br />Declare & initialize<br />intMyInt = 35; // Declaring and initializing the variable at the same time<br />
  26. 26. Operators<br />
  27. 27. Conditional structures<br />if statement<br /><ul><li>The if statement is entered when the given condition is true. Single-line case statements do not require block braces although it is mostly preferred by convention.</li></ul>Simple one-line statement:<br />if (i == 3) ... ;<br />Multi-line with else-block (without any braces):<br />if (i == 2)<br /> ...<br />else<br /> ...<br />
  28. 28. Conditional structures<br />switch statement<br /><ul><li>The switch construct serves as a filter for different values. </li></ul>switch (ch)<br />{<br />case 'A':<br /> ...<br /> break;<br /> case 'B':<br /> case 'C':<br /> ...<br /> break;<br /> default:<br /> ...<br /> break;<br />}<br />
  29. 29. Jump statements<br />The goto statement can be used in switch statements to jump from one case to another or to fall through from one case to the next.<br />switch(n)<br />{<br /> case 1:<br />Console.WriteLine("Case 1");<br /> break;<br /> case 2:<br />Console.WriteLine("Case 2");<br />goto case 1;<br /> case 3:<br />Console.WriteLine("Case 3");<br /> case 4: // Compilation will fail here as cases cannot fall through in C#.<br />Console.WriteLine("Case 4");<br />goto default; // This is the correct way to fall through to the next case.<br /> default:<br />Console.WriteLine("Default");<br />}<br />
  30. 30. Iteration structures<br />while loop<br />while (i == true)<br />{<br /> ...<br />}<br />do ... while loop<br />do<br />{<br /> ...<br />}<br />while (i == true);<br />for loop<br /><ul><li>The for loop consists of three parts: declaration, condition and increment. Any of them can be left out as they are optional.</li></ul>for (int i = 0; i < 10; i++)<br />{<br /> ...<br />}<br />
  31. 31. break statement<br />The break statement breaks out of the closest loop or switch statement. Execution continues in the statement after the terminated statement, if any.<br />int e = 10;<br />for (int i=0; i < e; i++)<br />{<br /> while (true)<br /> {<br /> break;<br /> }<br /> // Will break to this point.<br />}<br />
  32. 32. continue statement<br />The continue statement discontinues the current iteration of the current control statement and begins the next iteration.<br />intch;<br />while ((ch = GetChar()) >= 0)<br />{<br /> if (ch == ' ')<br /> continue; // Skips the rest of the while-loop<br /> // Rest of the while-loop<br /> ...<br />}<br />
  33. 33. Modifiers<br />Modifiers are keywords used to modify declarations of types and type members. Most notably there is a sub-group containing the access modifiers.<br /><ul><li>abstract - Specifies that a class only serves as a base class. It must be implemented in an inheriting class.
  34. 34. const - Specifies that a variable is a constant value that has to be initialized when it gets declared.
  35. 35. event - Declare an event.
  36. 36. extern - Specify that a method signature without a body uses a DLL-import.
  37. 37. override - Specify that a method or property declaration is an override of a virtual member or an implementation of a member of an abstract class.
  38. 38. readonly - Declare a field that can only be assigned values as part of the declaration or in a constructor in the same class.
  39. 39. sealed - Specifies that a class cannot be inherited.
  40. 40. static - Specifies that a member belongs to the class and not to a specific instance. (see section static)
  41. 41. unsafe - Specifies an unsafe context, which allows the use of pointers.
  42. 42. virtual - Specifies that a method or property declaration can be overridden by a derived class.
  43. 43. volatile - Specifies a field which may be modified by an external process and prevents an optimizing compiler from modifying the use of the field.</li></li></ul><li>FSHARP<br />F# is a multi-paradigm programming language, targeting the .NET Framework, that encompasses functional programming as well as imperative and object-oriented programming disciplines.<br />It is a variant of ML and is largely compatible with the OCaml implementation.<br />F# was initially developed by Don Syme at Microsoft Research but is now being developed at Microsoft Developer Division and is being distributed as a fully supported language in the .NET Framework and Visual Studio as part of Visual Studio 2010.<br />F# is a strongly typed language that uses type inference. <br />
  44. 44. F SHARP<br />F# uses pattern matching to resolve names into values. It is also used when accessing discriminated unions.<br />F# comes with a Microsoft Visual Studio language service that integrates it with the IDE.<br />All functions in F# are instances of the function type, and are immutable as well.<br />The F# extended type system is implemented as generic .NET types. <br />
  45. 45. Examples<br />A few small samples follow:<br />(* This is a comment *)<br />(* Sample hello world program *)<br />printfn "Hello World!"<br />
  46. 46. Operators<br />
  47. 47. Operators<br />
  48. 48. Functions <br />
  49. 49. Control Flow <br />
  50. 50. Control Flow<br />
  51. 51. Types <br />
  52. 52. Strings <br />
  53. 53. Strings<br />
  54. 54. Strings<br />
  55. 55. Booleans <br />
  56. 56. Mathematics <br />
  57. 57. Mathematics<br />
  58. 58. The End<br />Presented by:<br />Harry Kim Balois<br />BSCS 41A<br />