C # in Multan

Course Topics for Professional

Topic 1: What is C# Programming Language?

C# Introduction

 

It is an object-oriented programming language created by Microsoft that runs on the .NET Framework. C# has roots from the C family, and the language is close to other popular languages like C++ and Java. The first version was released in year 2002. The latest version, C# 8, was released in September 2019.C# is used for Mobile applications:

  • Desktop applications
  • Web applications
  • Web services
  • Web sites
  • Games
  • VR
  • Database applications
  • And much, much more!

Topic 2: C# Language Structure and C# Variables

Language Structure And Variables in C# Programming

The C# language is made up of variables and data types. Variables hold your values. You can either store that value in a database or display it to the user. Each variable has a data type, and you’re bound by syntax rules to only store a value that corresponds with its data type. This article uncovers variables and data types associated with these variables.

Variable Types

1- Floating points

2-Decimal

3-Boolean

4- Strings

Strings

String are an array of characters. Note that there is a difference between a “String” data type and a “string” data type. String – with the capital S – is a C# class. The string data type – lower case’s – is a primitive data type. You can do much more with the class String, but it’s generally frowned on by experienced developers to use the String class when the primitive data type can be used instead.Let’s take a look at how you can define and initialize a variable. Initializing and defining are two distinct processes in C#. First, take a look at a simple variable definition.

In the above code, two variables are defined. They are not initialized, because they have no value assigned to them. The first variable named my-number is defined as the int data type. You can only assign an integer value to this variable, which means any character or decimal value is rejected.

 

Topic 3: C# Operators

In this article, C# Operators, we will learn everything about different types of operators in C# programming language and how to use them. Operators are symbols that are used to perform operations on operands. Operators are symbols that are used to perform operations on operands.

For Examples

Operators are used to manipulate variables and values in a program. C# supports a number of operators that are classified based on the type of operations they perform.

1•Basic Assignment

Basic assignment operator (=) is used to assign values to variables. For example,

double x; x = 50.05;

Example


using System;

namespace Operator
{
	class Assignment Operator
	{
		public static void Main(string[] args)
		{
			int first Number, second Number;
			// Assigning a constant to variable
			first Number = 10;
			Console Write Line("First Number = {0}", first Number);

			// Assigning a variable to another variable
			second Number = first Number;
			Console Write Line("Second Number = {0}", second Number);
		}
	}
}

2. Arithmetic Operators

Arithmetic operators are used to perform arithmetic operations such as addition, subtraction, multiplication, division, etc.

For Example

int x = 5;
int y = 10;
int z = x + y;// z = 15

Topic 4: User Input

Most programs don’t just run and perform a function without any type of user input. User input is any click command, text from a keyboard, or entry in a form. In almost any program, you need to handle user input. You work with user input in console applications, local desktop applications, or your website pages. You need to handle user input, This article focuses on user input, how you can handle different data types, and returning a response to the user.

Console Input

The simplest C# program is a console application. These applications use the Microsoft command line to display information and take user input from the keyboard. Most beginner programmers learn how to deal with user input using the console, because these applications are the easiest to set up and configure. We’ll discuss input from web applications in later chapters.

C# requires two types of statements when you want to work with console input and output. The first statement is the output statement. This statement prompts the user for what must be entered. It also takes the calculated input and displays some information back to the user. For instance, you can create a simple calculator with a console application. You display a prompt asking the user to enter two numbers. The numbers are then added together and the result is displayed back to the user. Both the prompting statement and the resulting output are both a form of console output.

The second statement you need is the input statement. This statement stops execution of the application and waits for the user’s input. The user could close the application, which stops execution as well, but you assume in your code that the user will send some data for you to process. If the user decides to close the program, it does not crash the machine. It can cause issues if you store the information in a database, but we’ll assume that you’re just asking for input, performing a calculation, and then displaying the output to the user.

Topic 5: C# Strings

A string is an object of type String whose value is text. Internally, the text is stored as a sequential read-only collection of Char objects. There is no null-terminating character at the end of a C# string; therefore a C# string can contain any number of embedded null characters (‘\0’). The Length property of a string represents the number of Char objects it contains, not the number of Unicode characters. To access the individual Unicode code points in a string, use the StringInfo object.

string vs. System.String

In C#, the string keyword is an alias for String. Therefore, String and string are equivalent, and you can use whichever naming convention you prefer. The String class provides many methods for safely creating, manipulating, and comparing strings. In addition, the C# language overloads some operators to simplify common string operations. For more information about the keyword, see string. For more information about the type and its methods, see String.

Declaring and Initializing Strings

You can declare and initialize strings in various ways, as shown in the following example:

For Example
// Declare without initializing. string message1; // Initialize to null. string message2 = null; // Initialize as an empty string. // Use the Empty constant instead of the literal “”. string message3 = System.String.Empty; // Initialize with a regular string literal. string oldPath = “c:\\Program Files\\Microsoft Visual Studio 8.0”; // Initialize with a verbatim string literal. string newPath = @”c:\Program Files\Microsoft Visual Studio 9.0″; // Use System.String if you prefer. System.String greeting = “Hello World!”; // In local variables (i.e. within a method body) // you can use implicit typing. var temp = “I’m still a strongly-typed System.String!”; // Use a const string to prevent ‘message4’ from // being used to store another string value. const string message4 = “You can’t get rid of me!”; // Use the String constructor only when creating // a string from a char*, char[], or sbyte*. See // System.String documentation for details. char[] letters = { ‘A’, ‘B’, ‘C’ }; string alphabet = new string(letters);

Topic 6: Boolean Logic

Programs use simple comparisons to help make decisions. Boolean logic is a form of algebra where all values are either True or False. These values of true and false are used to test, the conditions that selection and iteration are based around.

Boolean logic uses algebra and algebraic expressions. We use these expressions in algorithms and programs.

Expression Boolean equivalent
Equals =
Greater than >
Less than <
Greater than or equal to >=
Less than or equal to <=
Does not equal <>
And AND
Or OR
Not NOT

Most programming languages use these equivalent Boolean expressions. However, some, such as Python, have slightly different equivalents: 

Topic 7: Loops and Arrays

Arrays help keep data organized, and their real power comes through when you are able to write a little code that can go through every element in an array. Instead of repeating code for each element, you just say:

for each element in array
do something

To go through an array in code, you can use an index variable to keep track of your position in the array, and increment it to get through the whole array. The For Loop works particularly well for such purposes:

//create array int[] array1 = {1, 2, 3, 4, 5, 6, 7, 8 }; //print each element in array for(int i = 0; i < array1.length; i = i+1){ System.out.println(array1[i]); }

This would create an array like above: {1, 2, 3, 4, 5, 6, 7, 8}

You can now see how a loop can find the largest number in an array:

static int getLargestNumber(int[] numbers){ int largest = 0; for(int i=0; i < numbers.length; i = i+1){ if(numbers[i] > largest){ largest = numbers[i]; } } return largest; }

 

Topic 8: C# Methods

last modified January 15, 2021 In this part of the tutorial, we cover C# methods. In object oriented programming, we work with objects. Objects are the basic building blocks of a program. Objects consists of data and methods. Methods change the state of the objects created. They are the dynamic part of the objects; data is the static part.C# Method definition

A method is a code block containing a series of statements. Methods must be declared within a class or a structure. It is a good programming practice that methods do only one specific task. Methods bring modularity to programs. Proper use of methods bring the following advantages:

  1. Reducing duplication of code
  2. Decomposing complex problems into simpler pieces
  3. Improving clarity of the code
  4. Reuse of code
  5. Information hiding

C# Method Characteristics

  1. Access level
  2. Return value type
  3. Method name
  4. Method parameters
  5. Parentheses
  6. Block of statements

Access level of methods is controlled with access modifiers. They set the visibility of methods. They determine who can call the method. Methods may return a value to the caller. In case our method returns a value, we provide its data type. If not, we use the void keyword to indicate that our method does not return values.

Topic 9: Inheritance and Polymorphism

Inheritance:

Inheritance is one in which a new class is created that inherits the properties of the already exist class. It supports the concept of code re usability and reduces the length of the code in object-oriented programming.

Types of Inheritance are:
  • Single inheritance
  • Multi-level inheritance
  • Multiple inheritance
  • Hybrid inheritance
  • Hierarchical inheritance

Inheritance:

Polymorphism is that in which we can perform a task in multiple forms or ways. It is applied to the functions or methods. Polymorphism allows the object to decide which form of the function to implement at compile-time as well as run-time.

Types of Polymorphism are:

  1. Compile-time polymorphism (Method overloading)
  2. Run-time polymorphism (Method Overriding) Hopefully that makes it clear as to what the differences are. Here is a comparison table.

Difference between Inheritance and Polymorphism:

INHERITANCE POLYMORPHISM
1. Inheritance is one in which a new class is created (derived class) that inherits the features from the already existing class(Base class). Whereas polymorphism is that which can be defined in multiple forms.
2. It is basically applied to classes. Whereas it is basically applied to functions or methods.
3. Inheritance supports the concept of reusability and reduces code length in object-oriented programming. Polymorphism allows the object to decide which form of the function to implement at compile-time (overloading) as well as run-time (overriding).
4. Inheritance can be single, hybrid, multiple, hierarchical and multilevel inheritance. Whereas it can be compiled-time polymorphism (overload) as well as run-time polymorphism (overriding).
5. It is used in pattern designing. While it is also used in pattern designing.

 

Topic 10: Interfaces and Abstract Classes in C#

As we all know that C# is an object oriented programming just like Java and provides full support for object-oriented concepts that are Encapsulation,  Inheritance, and Polymorphism. In contrast to Abstraction both Abstract class and Interface are coming out in picture as both of these provides abstraction in C# program.

In an abstract class, we can create the functionality and that needs to be implemented by the derived class. The derived class extend the interface and implement those functions.

Following are the important differences between Abstract Class and Inter

Sr. No. Key Abstract Class Interface
1 Definition In terms of standard definition an Abstract class is, conceptually, a class that a class cannot be instantiated and is usually implemented as a class that has one or more pure virtual (abstract) functions. On other hand an Interface is a description of what member functions must a class, which inherits this interface, implement. In other words, an interface describes behaviour of the class.
2 Implementation As like of other general class design in C# Abstract class also have its own implementation along with its declaration. On other hand an Interface can only have a signature, not the implementation. While its implementation is being provided by the class which implements it.
3 Inheritance As per specification in C# a class can extends only one other class hence multiple inheritance is not achieved by abstract class. On other hand in case of Interface a class can implements multiple interfaces and hence multiple inheritance is achieved by interface.
4 Constructor Like other classes in C# for instantiation abstract class also have constructor which provide an instance of abstract class to access its non-static methods. On other hand Interface do not have constructor so we can’t instantiate an interface directly although its method could get accessed by creating instance of class which implementing it.
5 Modifiers As abstract class is most like of other ordinary class in C# so it can contain different types of access modifiers like public, private, protected etc. On other hand as Interface needs to be get implemented in order to provide its methods implementation by other class so can only contains only contains public access modifier.
6 Performance As abstract class have its method as well as their implementations also for its abstract methods implementation it have reference for its implementing class so performance is comparatively faster as compare to that of Interface. On other hand the performance of interface is slow because it requires time to search actual method in the corresponding class.

 

 

Topic 11: Debugging and Enums

I’m not sure you can help, but I am at my wits end trying to sort out why my enum is not working. My main problem is that my test case works fine, but not when I use it in my larger program.

class TestTryEnum
{
static public void main( String[] args )
{
System.out.println( “Loose: ” + TryEnum.MyEnum.LOOSE.varOne() );
System.out.println( “Default: ” + TryEnum.MyEnum.DEFAULT.varOne() );
System.out.println( “Tight: ” + TryEnum.MyEnum.TIGHT.varOne() );

TryEnum.MyEnum myEnum = TryEnum.MyEnum.TIGHT;
System.out.println( “MyEnum: ” + myEnum.name() );
System.out.println( “MyEnum: ” + myEnum.varOne() );
System.out.println( “MyEnum: ” + myEnum.varTwo() );
}
}

interface TryEnum
{
public enum MyEnum
{
LOOSE ( “Low”, EnumConstants.lowOne, EnumConstants.lowTwo ),
DEFAULT ( “Default”, EnumConstants.defOne, EnumConstants.defTwo ),
TIGHT ( “High”, EnumConstants.higOne, EnumConstants.higTwo );

private String name;
private double varOne;
private double varTwo;

private MyEnum( String name, double varOne, double varTwo )
{
System.out.println( “SETTING NAME ” + name );
System.out.println( “SETTING VAR ONE ” + varOne );
System.out.println( “SETTING VAR TWO ” + varTwo );
this.name = name;
this.varOne = varOne;
this.varTwo = varTwo;
}

public String toString()
{
System.out.println( “IN TOSTRING” );
return name;
}

public double varOne()
{
System.out.println( “IN VARONE” );
return varOne;
}

public double varTwo()
{
System.out.println( “IN VARTWO” );
return varTwo;
}
}

Double do Something( MyEnum myEnum );
}

 

Topic 12: Generic Types

A major part of software engineering is building components that not only have well-defined and consistent APIs, but are also reusable. Components that are capable of working on the data of today as well as the data of tomorrow will give you the most flexible capabilities for building up large software systems.

In languages like C# and Java, one of the main tools in the toolbox for creating reusable components is generics, that is, being able to create a component that can work over a variety of types rather than a single one. This allows users to consume these components and use their own types.

Generic Class

Generic classes are defined using a type parameter in an angle brackets after the class name. The following defines a generic class.

for Example
class Data Store<T>
{
    public T Data { get; set; }
}

Above, the Data Store is a generic class. T is called type parameter, which can be used as a type of fields, properties, method parameters, return types, and delegates in the Data Store class. For example, Data, is generic property because we have used a type parameter T as its type instead of the specific data type.

Topic 13: Threading

 

 

 

Topic 14: Exception and File Handling

One of the advantages of C++ over C is Exception Handling. There are two types of exceptions: a)Synchronous, b)Asynchronous(Ex:which are beyond the program’s control, Disc failure etc). C++ provides following specialized keywords for this purpose.
try: represents a block of code that can throw an exception.
catch: represents a block of code that is executed when a particular exception is thrown.
throw: Used to throw an exception. Also used to list the exceptions that a function throws, but doesn’t handle itself.

Why Exception Handling?

Following are main advantages of exception handling over traditional error handling.

Exception Handling in C#

1) Following is a simple example to show exception handling in C++. The output of program explains flow of execution of try/catch blocks.

filter_noneedit

play_arrow

brightness_4

#include <iostream>
using namespace std;
int main()
{
   int x = -1;
   // Some code
   cout << "Before try \n";
   try {
      cout << "Inside try \n";
      if (x < 0)
      {
         throw x;
         cout << "After throw (Never executed) \n";
      }
   }
   catch (int x ) {
      cout << "Exception Caught \n";
   }
   cout << "After catch (Will be executed) \n";
   return 0;
}

2) There is a special catch block called ‘catch all’ catch(…) that can be used to catch all types of exceptions. For example, in the following program, an int  thrown as an exception, but there is no catch block for int, so catch(…) block will be executed.

CPP

filter_noneedit

play_arrow

brightness_4

#include <iostream>
using namespace std;
int main()
{
    try  {
       throw 10;
    }
    catch (char *excp)  {
        cout << "Caught " << excp;
    }
    catch (...)  {
        cout << "Default Exception\n";
    }
    return

Leave a Reply

Your email address will not be published. Required fields are marked *