Introduction to C#

Cards (45)

  • Introduction to C# Programming PRG261
  • 3 IMPORTANT INFORMATION DURATION weeks ASSESSMENTS 3 Class tests 1 Exam QUALIFYING MARK Summative Test 55% 25% Project M1 (15%) M2 (10%) 30% (CT1), 40% (CT2) Class Tests 1 Project (Milestone 1, Milestone 2)
  • Outcomes
    • Students should understand the following outcomes, upon successful completion of this module:
    • Intro to C# programming
    • Net Framework
    • Visual Studio IDE
    • Compiling
    • Creating first App
    • Getting Input and Producing Output
    • Data types
    • Operators
    • String manipulation
  • C#

    A modern, object-oriented programming language developed by Microsoft
  • C# was first released in 2002 and has since become a popular choice for developing a wide range of applications, including: desktop software (stand alone apps), web applications (web apps), games, and, mobile apps.
  • Basic concepts in C# programming

    • Variables and data types
    • Control structures
    • Classes and objects
    • Inheritance and polymorphism
    • Exception handling
  • Variables and data types

    In C#, variables are used to store data. C# has various data types, including int, double, bool, string, and char.
  • Control structures
    C# has various control structures that allow you to control the flow of your program, such as if-else statements, for loops, while loops, and switch statements.
  • Classes and objects

    C# is an object-oriented language, which means that you can create classes to define objects and their behavior. You can also use existing classes from libraries or frameworks.
  • Inheritance and polymorphism

    Inheritance allows you to create new classes based on existing classes, while polymorphism allows you to use objects of different classes in a uniform way.
  • Exception handling

    C# has built-in support for handling exceptions, which are errors that occur during program execution.
  • C# Compilation Process

    1. C# is not directly converted or compiled into machine-level language or machine instructions
    2. It is converted to an intermediate code known as Common Intermediate Language (CIL) or Intermediate Language Code (ILC or IL code)
    3. After converting the C# source code to CIL or ILC or IL code, the intermediate code needs to be converted to machine understandable code
    4. C# uses the .NET Framework and as part of this
  • .NET Framework
    • A Virtual Machine that manages the execution of C# programs
    • The virtual machine component is known as Common Language Runtime (CLR) which translates the CIL or IL code to native or machine code
    • This process is called the Just-In-Time (JIT) or Dynamic Compilation which is the way of compiling code during the execution of a program at run time only
  • .NET Framework Features
    • Cross- Language Interoperability
    • Multi-language Support
    • Automatic Resource Management
    • Type Safety
    • Debugging
  • Cross-Language Interoperability

    The ability of a code to interact with another code that is written by using a different programming language irrespective of the language in which it is created
  • Multi-language Support

    The .NET platform supports many programming languages. A new compiler must be implemented for each language, FrameworkV2.0 supports up to 45 languages.
  • Automatic Resource Management

    The .NET Framework's garbage collector manages the allocation and release of memory for our application. Each time we create a new object, the common language runtime allocates memory for the object.
  • Type Safety
    C# does not allow one data type to be assigned to other data type. This means that some code can consume types and instances declared in other languages.
  • Debugging
    A debugger is a computer program that lets you run your program, line by line and examine the values of variables or look at values passed into methods and let you figure out why it isn't running the way you expected it to.
  • Visual Studio IDE
  • C# Code Structure
  • Compile and Run C# Program

    1. Click on Start to compile and execute program
    2. Output:
  • C# Input and Output

    1. Console.Write("Enter Name: ")
    2. string fName = Console.ReadLine()
    3. Console.Write("Enter Student Number: ")
    4. int sNumber = int.Parse(Console.ReadLine())
    5. Console.WriteLine("Enter Student fees: ")
    6. double fees = double.Parse(Console.ReadLine())
    7. Console.WriteLine("Are you Diploma?: ")
    8. bool check = bool.Parse(Console.ReadLine())
    9. Console.WriteLine()
    10. Console.WriteLine("Student: " + fName + " has student number: " + sNumber)
    11. Console.WriteLine("Student is in diploma: " + check + " and pays fees: " + fees)
    12. Console.ReadKey()
  • Write Line() & Write() methods

    1. Console.WriteLine("Different...")
    2. Console.WriteLine("..Lines")
    3. Console.Write("Same...")
    4. Console.Write("...Line")
    5. Console.ReadLine()
  • ReadLine()

    The ReadLine() method reads the next line of input from the standard input stream. It returns the same string.
  • Read()

    The Read() method reads the next character from the standard input stream. It returns the ascii value of the character.
  • ReadKey()

    The ReadKey() method obtains the next key pressed by user. This method is usually used to hold the screen until user press a key.
  • int.Parse

    A method in C# that is used to convert a string of a number to its integer equivalent.
  • Convert.ToInt32

    A method in C# that is used to convert a string of a number to its integer equivalent. It allows null values and doesn't throw any errors, unlike int.Parse().
  • Data Types

    • Value types
    • Reference types
  • Value types

    Value types are data types that store their values directly in memory. When you declare an int type, the system allocates memory to store the value.
  • Reference types

    Reference types are data types that store a reference (or pointer) to an object in memory. They use multiple variables, the reference types can refer to a memory location. If the data in the memory location is changed by one of the variables, the other variable automatically reflects this change in value.
  • C# - Type Conversion

    • Implicit type conversion
    • Explicit type conversion
  • Implicit type conversion

    These conversions are performed by C# in a type-safe manner. For example, are conversions from smaller to larger data types.
  • Explicit type conversion

    These conversions are done explicitly by users using the pre-defined functions. Explicit conversions require a cast operator.
  • C# - Operators

    • Arithmetic Operators
    • Relational Operators
    • Logical Operators
    • Bitwise Operators
  • Arithmetic Operators

    Operators used for mathematical calculations
  • Relational Operators

    Operators used for comparing values
  • Logical Operators

    Operators used for logical operations
  • Bitwise Operators

    Operators that work on bits and perform bit by bit operation