0% found this document useful (0 votes)
159 views7 pages

C# Visual Programming for Class 12

The document provides an overview of C#, a modern object-oriented programming language developed by Microsoft, highlighting its features and applications. It includes practical programming examples such as an if-else ladder, for loop, multi-dimensional array, string manipulation, and a simple calculator using a switch statement. Each program is accompanied by explanations of its functionality and purpose.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
159 views7 pages

C# Visual Programming for Class 12

The document provides an overview of C#, a modern object-oriented programming language developed by Microsoft, highlighting its features and applications. It includes practical programming examples such as an if-else ladder, for loop, multi-dimensional array, string manipulation, and a simple calculator using a switch statement. Each program is accompanied by explanations of its functionality and purpose.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Visual Programming Practical Sheet for class 12

Definition of C#:

C# (pronounced "C-sharp") is a modern, object-oriented programming language developed


by Microsoft as part of the .NET framework. It is used for building a wide range of
applications, including desktop, web, and mobile applications. C# is known for its
simplicity, powerful features, and compatibility with Windows-based systems.

Development of C#.NET

• C# was created by Anders Hejlsberg in 2000 as part of Microsoft’s .NET initiative.

Features of C#.NET

• Object-Oriented: Supports classes, objects, inheritance, polymorphism.

• Type-Safe: Ensures data types are correct.

• Rich Library: Extensive library for handling files, databases, networking, etc.

Program 1: If-Else Ladder

This program demonstrates the use of multiple conditions using if, else if, and else.

using System;

class Program

static void Main()

int x = 20;

if (x > 50)
{

[Link]("Greater than 50");

else if (x == 20)

[Link]("Equal to 20");

else

[Link]("Less than 20");

Explanation:

• The program checks whether the value of x is greater than 50, equal to 20, or less
than 20 and prints the corresponding message.

Program 2: For Loop

This program uses a for loop to print the first 5 numbers.

using System;

class Program

static void Main()

// Print numbers from 0 to 4


for (int i = 0; i < 5; i++)

[Link](i);

Explanation:

• The for loop starts from i = 0 and runs until i reaches 4, printing the value of i at each
iteration.

Program 3: Multi-dimensional Array

This program demonstrates the use of a 2D array (matrix) in C#.

using System;

class Program

static void Main()

// Declare and initialize a 2x2 matrix

int[,] matrix = { { 1, 2 }, { 3, 4 } };

// Access and display elements from the matrix

[Link]("Matrix Elements:");

for (int i = 0; i < 2; i++)

{
for (int j = 0; j < 2; j++)

[Link](matrix[i, j] + " ");

[Link]();

Explanation:

• This program creates a 2x2 matrix and prints the elements of the matrix row by row.

Program 4: String Manipulation

This program demonstrates the use of string methods to manipulate and display a string.

using System;

class Program

static void Main()

// Create a string

string message = "Hello, C# World!";

// Convert to uppercase

string upperMessage = [Link]();

[Link]("Uppercase: " + upperMessage);


// Get substring from the message

string substring = [Link](7, 3);

[Link]("Substring: " + substring);

// Replace characters in the string

string replacedMessage = [Link]("World", "Universe");

[Link]("Replaced Message: " + replacedMessage);

Explanation:

• The program shows how to manipulate a string by converting it to uppercase,


extracting a substring, and replacing a word in the string.

Program 5: Simple Calculator Using Switch Statement

This program demonstrates the use of a switch statement to perform basic arithmetic
operations based on user input.

using System;

class Program

static void Main()

double num1, num2, result;

string operation;
// User input for numbers and operation

[Link]("Enter first number:");

num1 = [Link]([Link]());

[Link]("Enter second number:");

num2 = [Link]([Link]());

[Link]("Enter operation (+, -, *, /):");

operation = [Link]();

// Switch statement to perform the operation

switch (operation)

case "+":

result = num1 + num2;

[Link]("Result: " + result);

break;

case "-":

result = num1 - num2;

[Link]("Result: " + result);

break;

case "*":

result = num1 * num2;

[Link]("Result: " + result);


break;

case "/":

if (num2 != 0)

result = num1 / num2;

[Link]("Result: " + result);

else

[Link]("Error: Division by zero is not allowed.");

break;

default:

[Link]("Invalid operation entered.");

break;

Explanation:

• The program prompts the user for two numbers and a mathematical operator.

• It uses a switch statement to perform the correct operation based on the user's
input (+, -, *, or /).

• If the user enters an invalid operation or attempts to divide by zero, it handles the
error accordingly

Common questions

Powered by AI

C#'s object-oriented nature facilitates platform-agnostic application development by allowing developers to create reusable components that can be adapted for different platforms. Classes, objects, and methods provide a structured approach to organize and implement functionalities that can be ported easily. C# is part of the .NET framework, which offers a comprehensive library and runtime that supports web, desktop, and mobile environments, enabling developers to maintain consistent, robust, and scalable application logic across different platforms .

C# within the .NET framework leverages the framework's extensive library to simplify complex applications by providing pre-built classes and functions that manage lower-level operations like networking, file handling, and database interaction. This abstraction allows developers to focus on specific business logic rather than infrastructure code, reducing development time and complexity. The framework's cross-language interoperability and integrated development environments enhance productivity, making it easier to build robust, scalable applications efficiently .

C# ensures type safety by enforcing strict type checking, meaning that data types are verified at compile time to prevent type errors during runtime. This prevents operations on incompatible data types, protecting against common programming errors. Type safety is crucial in software development because it increases program reliability and security, reduces the chances of runtime exceptions, and helps maintain data integrity .

Using a switch statement can be advantageous over an if-else ladder when dealing with multiple discrete values. It simplifies code readability and maintainability by providing a clear map of operations based on specific input values. Switch statements also allow for better optimization by the compiler since each branch is examined at compile time. This is especially beneficial for scenarios such as menu-driven programs or handling various modes or options where choices map directly to particular actions .

Inheritance is an important feature of C# as it allows the creation of a new class based on an existing class. This promotes code reusability and modularity by enabling new classes to inherit existing functionality without rewriting code, facilitating extension and maintenance. Inheritance helps in creating a hierarchical structure of classes which can be leveraged to implement polymorphism, allowing for more flexible and dynamic method calling across derived classes .

Multidimensional arrays in C# provide a structured way to store and manipulate tabular data efficiently, allowing for compact code representation of data grids and matrices. This aids in simplifying complex operations such as mathematical computations or table manipulations. However, they can be prone to pitfalls like increased memory consumption and complexity in indexing, potentially leading to errors if indices are mismanaged. Careful consideration of array size and dimensionality is crucial, as these factors greatly influence performance and ease of use .

Global variables are accessible throughout the entire program, which can lead to unintended side-effects and make debugging more difficult due to potential modifications from any part of the code. Local variables, confined to the method scope, promote better data encapsulation and reduce the risk of accidental interference. Using local variables enhances memory management as they are freed when the method execution ends. Therefore, while global variables offer convenience in data sharing across functions, local variables are preferable for maintaining modular, reliable, and maintainable code structures .

Interface-based design in C# enhances flexibility and maintainability by allowing developers to decouple implementation details from the object's behavior. Interfaces define contract-based programming, enabling different classes to implement the same interface, providing a consistent method set. This abstraction promotes plugin architecture, where functions can be added or swapped without modifying existing code, allowing for easier updates and scalability. Interface-based design is crucial for creating flexible systems that adapt to change and extend functionality with minimal impact on existing classes .

The primary difference between a for loop and a while loop is in their syntax and typical use cases. A for loop is best used when the number of iterations is known beforehand, as it combines initialization, condition-checking, and iteration increment in a single line, enhancing readability. A while loop is better suited for scenarios where the number of iterations is not predetermined, as it only checks the condition before each iteration. For loops are often preferred for iterating over arrays or collecting known sizes, whereas while loops are suitable for reading inputs until a particular condition is met .

String immutability in C# means that once a string is created, its value cannot be altered. This results in a new string being allocated in memory every time a manipulation occurs, such as concatenation or replacement. While this provides benefits like thread-safety and predictable behavior, it can impact memory management by leading to inefficiencies through excessive allocation and deallocation in memory-intensive applications. Developers often use the StringBuilder class to handle many modifications to avoid these drawbacks .

You might also like