C# | C Sharp Tutorial

The Function that lacks a specific name is called Anonymous Functions. There are two types of Anonymous Functions available in C#

  • Lambda Expressions
  • Anonymous Methods

Example:

using System;
namespace LambdaExpressions
{
    class deneme
    {
        delegate int Square(int num);
        static void Main(string[] args)
        {
            Square GetSquare = x => x * x;
            int j = GetSquare(15);
            Console.WriteLine("Square: " + j);
        }
    }
}
//Output:Square: 225

C# for Loop

Here, you will learn how to execute a statement or code block multiple times using the for loop, structure of the for loop, nested for loops, and how to exit from the for loop.

The for keyword indicates a loop in C#. The for loop executes a block of statements repeatedly until the specified condition returns false.

Syntax:
for (initializer; condition; iterator)
{
    //code block 
}

The following for loop executes a code block 10 times.

Example: for Loop
for(int i = 0; i < 10; i++)
{
    Console.WriteLine("Value of i: {0}", i);
}
Output:
Value of i: 0
Value of i: 1
Value of i: 2
Value of i: 3
Value of i: 4
Value of i: 5
Value of i: 6
Value of i: 7
Value of i: 8
Value of i: 9

The control variable for the for loop can be of any numeric data type, such as double, decimal, etc.

Example: Decimal for Loop
for (double d = 1.01D; d < 1.10; d+= 0.01D)
{
    Console.WriteLine("Value of i: {0}", d);
}

Value of i: 1.01
Value of i: 1.02
Value of i: 1.03
Value of i: 1.04
Value of i: 1.05
Value of i: 1.06
Value of i: 1.07
Value of i: 1.08
Value of i: 1.09

C# while and do...while loop

The while keyword is used to create while loop in C#. The syntax for while loop is:

while (test-expression)
{
	// body of while
}

How while loop works?

  1. C# while loop consists of a test-expression.
  2. If the test-expression is evaluated to true,
    1. statements inside the while loop are executed.
    2. after execution, the test-expression is evaluated again.

while loop Flowchart


Example 1: while Loop

using System;
namespace Loop
 {
class WhileLoop
 {
public static void Main(string[] args)
{
int i=1; while (i<=5)
 {
Console.WriteLine("C# For Loop: Iteration {0}", i); i++;
}
 }
 }
}
When we run the program, the output will be:
C# For Loop:Iteration 1
C# For Loop: Iteration 2
C# For Loop: Iteration 3
C# For Loop: Iteration 4
C# For Loop: Iteration 5

C# do...while loop

The do and while keyword is used to create a do...while loop. It is similar to a while loop, however there is a major difference between them.

The syntax for do...while loop is:

do
{
	// body of do while loop
} while (test-expression);

Example : do...while loop

using System;
namespace Loop
 {
 class DoWhileLoop
 {
public static void Main(string[] args)
{
 int i = 1, n = 5, product;
do
 {
 product = n * i;
Console.WriteLine("{0} * {1} = {2}", n, i, product); i++;
}
while (i <= 10);
 }
 }
}
When we run the program, the output will be:
5 * 1 = 5
5 * 2 = 10
 5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
 5 * 6 = 30
 5 * 7 = 35
5 * 8 = 40
 5 * 9 = 45
 5 * 10 = 50

C# – Graphics – Display an Image

In this Article, I am going to explain, how to display an image using C# code.

ets’ create a sample Windows Forms Application.

Step 1. Create a Windows Forms Application. You can refer this Article “Visual Studio: Creating a project“.

Step 2. From a Toolbox, add a button to the Form. Double click on the button, to add a button click handler.

Step 3. Inside the button click handler, add below code:

  • How do we get Graphics object? Below statement will get the Graphics object:
Graphics g = this.CreateGraphics();
  • Image class – Image class provides useful methods to load the images into the memory. How do we load the image from the file? Below statement will help to load the image from the file and create an Image object.
Image img = Image.FromFile(@"C:\Users\Public\Pictures\Sample Pictures\Koala.jpg");
  • The image will be resized, if the image size is more or less than the mentioned rectangular area.
g.DrawImage(img, 0, 0, this.Width, this.Height);

Here is the code, added into the button click event handler.

        private void btnDraw_Click(object sender, EventArgs e)
        {
            Image img = Image.FromFile(@"C:\Users\Public\Pictures\Sample Pictures\Koala.jpg");

            Graphics g = this.CreateGraphics();
            g.DrawImage(img, 0, 0, this.Width, this.Height);

            g.Dispose();
        }

Step 4. Compile & Run the Application. Click on the button and you will see an Image drawn on the Application window.

using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
using System.Drawing;
using System.Linq;
 using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; namespace DrawImage { public partial class frmDrawImage : Form
{
Image img = null; public frmDrawImage()
{
InitializeComponent();
 }
private void btnDraw_Click(object sender, EventArgs e)
{
if ( img == null )
 img = Image.FromFile(@"C:\Users\Public\Pictures\Sample Pictures\Koala.jpg");
 }
private void frmDrawImage_Paint(object sender, PaintEventArgs e)
 {
if (img != null)
{
Graphics g = e.Graphics; g.DrawImage(img, 0, 0, this.Width, this.Height);
g.Dispose();
 }
 }
 }
}

Drawing Text with C#

Text is drawn onto a Graphics Object using the DrawText() method. The syntax for this method is as follows:

graphicsobj.DrawString(string, font, brush, x, y);

In order to create a Font object a font size, font family and font style may be specified. For example to create a Helvetica, 40 point Italic font:

       Font myFont = new System.Drawing.Font("Helvetica", 40, FontStyle.Italic); 

A brush object is created by specifying by calling the appropriate constructor for the brush type and specifying a color:

       Brush myBrush = new SolidBrush(System.Drawing.Color.Red);

Having created the necessary objects we can incorporate these into our example C# application to draw some text:

       private void Form1_Paint(object sender, PaintEventArgs e)
        {
            System.Drawing.Graphics graphicsObj;

            graphicsObj = this.CreateGraphics();

            Font myFont = new System.Drawing.Font("Helvetica", 40, FontStyle.Italic);

            Brush myBrush = new SolidBrush(System.Drawing.Color.Red);

            graphicsObj.DrawString("Hello C#", myFont, myBrush, 30, 30);

        }



                                                       BACK   Next