0% found this document useful (0 votes)
35 views

1D-Array: Namespace Public Partial Class Public

This document contains code examples demonstrating 1D arrays, 2D arrays, structures, and inheritance with overriding in C#. The 1D array example shows adding elements to an array and displaying the contents. The 2D array example demonstrates initializing a 2D array, adding elements, displaying the contents, and setting elements to zero. The structure example defines a simple structure with a property. The inheritance example defines base and derived classes where the derived classes override the base class method.

Uploaded by

Abhishek Kumar
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

1D-Array: Namespace Public Partial Class Public

This document contains code examples demonstrating 1D arrays, 2D arrays, structures, and inheritance with overriding in C#. The 1D array example shows adding elements to an array and displaying the contents. The 2D array example demonstrates initializing a 2D array, adding elements, displaying the contents, and setting elements to zero. The structure example defines a simple structure with a property. The inheritance example defines base and derived classes where the derived classes override the base class method.

Uploaded by

Abhishek Kumar
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

1D-Array

namespace _1DArray { public partial class Form1 : Form { public Form1() { InitializeComponent(); label2.Text = ""; } int index = 0; int[] arr = new int[5]; private void button1_Click(object sender, EventArgs e) { if (index > 4) { MessageBox.Show("Array is full"); textBox1.Text = ""; } else { arr[index] = Convert.ToInt32(textBox1.Text); label2.Text = label2.Text + arr[index] + " "; index++; textBox1.Text = ""; textBox1.Focus();

} } } }

2D-Array
*************************************************************************************
namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } int r = 0, c = 0; int[,] ar = new int[3, 3]; int cs = 3, rs = 3; private void button1_Click(object sender, EventArgs e) { if (r == rs) { MessageBox.Show("Array is Full"); for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { label2.Text += Convert.ToInt32(ar[i, j]) + " "; } label2.Text += "\n"; } for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (i != j) { ar[i, j] = ar[i, j] * 0; } } } for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { label3.Text += Convert.ToInt32(ar[i, j]) + " "; } label3.Text += "\n"; } } else if (c < cs && r < rs) { ar[r, c] = Convert.ToInt32(textBox1.Text);

c++; textBox1.Text = ""; textBox1.Focus(); } else { c = 0; r = r + 1; } } } }

*************************************************************************************

namespace MultiArray { public partial class Form1 : Form { public Form1() { InitializeComponent(); } int[,] arr=new int[4,4]; private void button1_Click(object sender, EventArgs e) { label1.Text = ""; for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { arr[i, j] = Convert.ToInt32(textBox1.Text); textBox1.Text = ""; } } for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { label1.Text = label1.Text + arr[i, j] + " } label1.Text = label1.Text + "\n"; } }

";

private void Form1_Load(object sender, EventArgs e) {

} } }

*************************************************************************************

Structure
using System; struct SimpleStruct { private int xval; public int X { get { return xval; } set { if (value < 100) xval = value; } }

public void DisplayX() { Console.WriteLine("The stored value is: {0}", xval); } }

class TestClass { public static void Main() { SimpleStruct ss = new SimpleStruct(); ss.X = 5; ss.DisplayX(); } }

Inheritance (Overriding)
namespace InheriandClassWin { public partial class Form2 : Form { public Form2()

{ InitializeComponent(); }

class Shape { protected int width, height; public Shape(int a, int b) { width = a; height = b; } public virtual void area() { Console.WriteLine("Parent class area :");

} } class Rectangle : Shape { public Rectangle(int a = 0, int b = 0) : base(a, b) {

public override void area() { //Console.WriteLine("Rectangle class area :");

MessageBox.Show(Convert.ToString (width * height)); } } class Triangle : Shape { public Triangle(int a, int b) : base(a, b) {

} public override void area() { Console.WriteLine("Triangle class area :"); MessageBox.Show(Convert.ToString(width * height/2));

} }

private void button1_Click(object sender, EventArgs e) { if (comboBox1.SelectedIndex == 0)

{ Triangle t = new Triangle(Convert.ToInt32(textBox1.Text), Convert.ToInt32(textBox2.Text)); t.area(); } else { Rectangle r = new Rectangle(Convert.ToInt32(textBox1.Text), Convert.ToInt32(textBox2.Text)); r.area(); } } } }

You might also like