Part 11 – C# Tutorial – switch statement in C#

22
2



Text version of the video

Slides

All C# Text Articles

All C# Slides

All Dot Net and SQL Server Tutorials in English

All Dot Net and SQL Server Tutorials in Arabic

In this part of the c sharp tutorial we will learn
1. switch statement
2. break statement
3. goto statement

Nguồn:https://wijstaanvooronzegrondrechten.org/

22 COMMENTS

  1. I saw your mvc tutorials ,asp.net core tutorials and also c# the all stuff is very helpful.
    But now I want to zamrine please can you start making tutorials on a zamrine mobile application using dot net

  2. For if condition i can use logical operator and check " i and s " how can i use in switch case please let me know

    if (i == 1&& s=="a")
    {
    Console.WriteLine("your num is 1 & a");
    }

    how to code above in switch condition???????

  3. using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace ConsoleApplication54
    {
    class Program
    {
    static void Main(string[] args)
    {
    var game = new GameOfLife(0, 0);
    game.insertRandomLives();
    Console.WriteLine(game.DrawGeneration);
    for (;;)
    {
    game.ApplyRUlesAndNewG();
    System.Threading.Thread.Sleep(1000);
    Console.Clear();
    Console.WriteLine(game.DrawGeneration);
    }
    }
    }
    class GameOfLife
    {
    bool[,] Generation;

    Random random = new Random();

    public bool DrawGeneration { get; internal set; }

    public GameOfLife(int numberofrows, int numberofculums)
    {
    //NR= numberofrows:)
    //NC= numberofculums:)
    {
    CreateFirstGeneration(numberofrows, numberofculums);
    }
    }

    private void CreateFirstGeneration(int rows, int cols)
    {
    if (rows <= 0 || cols <= 0)
    {
    rows = 21;
    cols = 21;
    }
    Generation = new bool[rows, cols];
    }

    public void insertRandomLives() => Iterator((row, col) => { G[row, col] = random.Next(0, 10) == 1; });

    public int CountNeighbours(int rows, int col)
    {
    var count = default(int);
    count += rows + 1 < Generation.GetLength(0) && Generation[rows + 1, col] ? 1 : 0;
    count += rows – 1 >= Generation.GetLowerBound(0) && Generation[rows – 1, col] ? 1 : 0;
    count += col + 1 <= Generation.GetLength(1) && Generation[rows, col + 1] ? 1 : 0;
    count += col – 1 >= Generation.GetLowerBound(1) && Generation[rows, col – 1] ? 1 : 0;
    count += col + 1 <= Generation.GetLength(0) && col + 1 < Generation.GetLength(1) && Generation[rows + 1, col + 1] ? 1 : 0;
    count += col – 1 <= Generation.GetLowerBound(0) && col – 1 < Generation.GetLowerBound(1) && Generation[rows – 1, col – 1] ? 1 : 0;
    count += col + 1 <= Generation.GetLength(0) && col – 1 < Generation.GetLength(1) && Generation[rows + 1, col – 1] ? 1 : 0;
    count += col – 1 <= Generation.GetLowerBound(0) && col + 1 < Generation.GetLength(1) && Generation[rows – 1, col + 1] ? 1 : 0;

    return count;

    }

    public void ApplyRUlesAndNewG()
    {
    var newGen = new bool[Generation.GetLength(0), Generation.GetLength(1)];
    Iterator((row, col)) =>
    {
    if (Generation[row, col])
    {
    if (CountNeighbours(row, col) > 3)

    {
    newGen[row, col] = !Generation[row, col];
    else if (CountNeighbours(rows, col) == 3 || CountNeighbours(rows, col) == 4 || CountNeighbours(rows, col) == 5)
    {
    newGen[row, col] = Generation[row, col];
    else if (CountNeighbours(rows, col) > 5)
    {
    newGen[row, col] = !Generation[row, col];

    else if (CountNeighbours(rows, col) == 5)
    {
    newGen[row, col] = !Generation[row, col];
    }

    }
    }
    }
    }
    }
    Generation = (bool[,])newGen.Clone();
    }

    }

    public string DrawGeneration()
    {
    var builder = new StringBuilder();
    Interator((row, col)) =>
    {
    builder.Append(Generation[row, col] ? "*" : " ");
    })
    () =>
    {
    builder.AppendLine();
    })
    return builder.ToString();

    }

    public void Interator(Func<object, object, object> p)
    {
    throw new NotImplementedException();
    }

    private void Iterator(Action<int, int> statement, Action statement2 = null)
    {
    for (var row = 0; row < Generation.GetLength(0); row++)
    {
    for (var col = 0; col < Generation.GetLength(1); col++)
    {
    statement(row, col);
    }
    if (statement2 != null)
    {
    statement2();
    }

    }
    }
    }

    what i did wrong

    ?

  4. The if/else portion fails when the user users a non numerical input on the console. Should read input as a string then confirm it's a number.

    Console.WriteLine("Please Enter a Number: ");
    string UserInputString = Console.ReadLine();
    int UserInputNumber;

    //confirm input is a number
    int.TryParse(UserInputString, out UserInputNumber);

    if (UserInputNumber == 10)
    Console.WriteLine("Your Number is 10");
    else if (UserInputNumber == 20)
    Console.WriteLine("Your Number is 20");
    else if (UserInputNumber == 30)
    Console.WriteLine("Your Number is 30");
    else
    Console.WriteLine("Your Number is not 10 20 30");

  5. i got this error..plz..give answr…for below program

    Error 1 Could not find an implementation of the query pattern for source type 'string[]'.  'Join' not found.  Are you missing a reference or a using directive for 'System.Linq'?

    program::;;;

    using System;
        class Program
        {
            static void Main()
            {
                String[] types = { "meat", "dairy", "veg", "fruits" };
                var prods = new[]
                {
                    new {Product = "beaf",Category = "meat"},
                    new {Product = "toilet paper",Category = "houshold"},
                    new {Product = "milk",Category = "dairy"},
                    new {Product = "candybar",Category = "snack"}
                };
                var q = from t in types
                        join p in prods on t equals p.Category
                        select new { Category = t, p.Product };
                foreach (var v in q)
                   Console.WriteLine("{0}:{1}",v.Product,v.Category);

            }
        }
        

LEAVE A REPLY

Please enter your comment!
Please enter your name here