C# Course – codemeets– 05

c sharp lesson 5. In this lesson, we will dive into 2D Arrays, Classes, Objects, and Interface Design. These are important concepts that will give you a deeper understanding of object-oriented programming and data structures in C#.

2D Arrays in C#

A two-dimensional array in C# is declared using two pairs of square brackets, such as int[,] or string[,]. It is essentially an array of arrays, where data is stored in a grid-like structure with rows and columns.

You can get Idea Using Example Below about 2d arrays.

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int[,] cat =
            {
                {1,2},
                {4,6},
                {8,9}
            };
            Console.WriteLine(cat[0,1]);
            Console.ReadLine();
        }
    }
}

This code Answer is 2.

Object & Class in c sharp

Class is collection of objects and functions.programming class

In computer programming, a function object is a construct that allows an object with the same syntax to be called or called a normal function. Active objects are often referred to as funksters.Object in programming

In object-oriented programming (OOP), a class is a blueprint for creating objects, providing initial values for state (member variables) and implementations of behavior (member functions or methods). The use of classes revolutionized the software development industry by enabling faster and more efficient software development, thanks to concepts like code reuse and modularity.

Class in C#:

In C#, classes allow you to group related data and methods together into a single unit, making it easier to design, manage, and scale applications. The special feature of classes is their ability to reuse code, which leads to cleaner, more maintainable software.

Creating and Using a Class in C#:

To declare a class, you need to use the class keyword followed by the class name. Inside the class, you can define member variables (fields), constructors, and methods (functions).

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

namespace ConsoleApplication1
{
    class Program
    {
        class book{                    // Create Class
            public String title;
            public String Author; 
            public int pages;
        }
        static void Main(string[] args)
        {
            book book1 = new book();     // Call Class
            book1.title = "Harry Potter";
            book1.Author = "JK";
            book1.pages = 450;

            Console.WriteLine(book1.pages);
            Console.ReadLine();
        }
    }
}

You can see this code do create class and call class.

Now show you how to work class using method look below c sharp example.

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

namespace ConsoleApplication1
{
    class Program
    {
        public  Book (String name, int pages)
        {
            name = aname;
            pages = apages;
        }
        public bool hash()
        {
            if (pages > 200)
            {
                return true;
            }
            false{
                return false;
            }
        }
        static void Main(string[] args)
        {
            Book Book1 = new Book("magic", 280);
            Console.WriteLine(Book1.hash());
            Console.ReadLine();
        }
    }
}

This code output is True.

Work with interface in C#

It looks like you’re working on a Windows Forms Application in C# and want to use an interface. However, your instructions describe setting up a simple Windows Forms application that uses basic controls like Label, TextBox, and Button. Let’s break this down into steps to guide you through the process of creating a simple form that takes user input and displays it on a label.

Steps to Create a Simple Windows Forms Application in C#:

  1. Create a New Project:
    • Open Visual Studio.
    • Go to File > New > Project.
    • Choose Windows Forms App (.NET Framework) (if using .NET Framework) or Windows Forms App (for .NET Core/5+).
    • Click OK to create the project.
  2. Design the Form:
    • On the left side, you’ll find the Toolbox.
    • Drag and Drop a Label onto the form.
      • In the Properties window (usually on the right), you can change the Text property to something like “Enter Your Name:”.
    • Drag and Drop a TextBox next to the label. This is where the user will type in their input.
    • Drag and Drop a Button below the TextBox. This button will trigger the action to show the input.
  3. Set Properties:
    • For the Label, change the Text to something like "Enter your name:".
    • For the Button, change the Text property to "Submit".
    • Optionally, set a name for each control for easier reference in the code, such as label1, textBox1, and button1.
  4. Code the Button’s Click Event:
    • Double-click the button to open the code editor.
    • In the code editor, inside the button1_Click method, type the following code:
csharpCopyEditprivate void button1_Click(object sender, EventArgs e)
{
    label1.Text = "Hello " + textBox1.Text;
}

Full Example Code:

csharpCopyEditusing System;
using System.Windows.Forms;

namespace WindowsFormsApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            label1.Text = "Hello " + textBox1.Text;  // Display input text on the label
        }
    }
}

Explanation:

  • Button Click Event: When the button is clicked, the button1_Click event is triggered, and the text entered in textBox1 is shown on the label1.
  • Dynamic Update: The label1.Text is updated dynamically to greet the user with the input text (e.g., "Hello John" if the user enters “John” in the text box).

Result:

When you run the application:

  • The user will enter their name in the TextBox.
  • When the Submit button is clicked, the Label will update to display “Hello” followed by the entered name.

This is a simple Windows Forms application where you dynamically change the label text based on user input.

Now over the C# basic course. you go this link and answer the question and get C# certificate. Get Certificate

Thank for all!

More C# ClickHere!

See you Next Lesson Soon!

You may also like...

Leave a Reply

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

Translate »