0% found this document useful (0 votes)
8 views2 pages

C# Programming Concepts and Examples

The document contains multiple C# code examples demonstrating various programming concepts, including command line arguments, branching, looping, switching, swapping numbers by value and reference, type casting, boxing and unboxing, inheritance, interfaces, sealed classes, abstract classes, partial classes, and exception handling. Each example illustrates a specific feature or concept in C#. The code snippets are structured to provide clear demonstrations of each topic.
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)
8 views2 pages

C# Programming Concepts and Examples

The document contains multiple C# code examples demonstrating various programming concepts, including command line arguments, branching, looping, switching, swapping numbers by value and reference, type casting, boxing and unboxing, inheritance, interfaces, sealed classes, abstract classes, partial classes, and exception handling. Each example illustrates a specific feature or concept in C#. The code snippets are structured to provide clear demonstrations of each topic.
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

1.

C# program to print Hello World using command line argument


using System; class HelloWorld { static void Main(string[] args) { if ([Link] >
0) [Link]("Hello, " + args[0] + "!"); else [Link]("Hello
World!"); } }

2. Console application demonstrating switching, looping, branching


using System; class Demo { static void Main() { int x = 5; // Branching if (x > 0)
[Link]("Positive number"); else [Link]("Negative number"); //
Looping for (int i = 1; i <= 5; i++) [Link]("Number: " + i); // Switching
switch (x) { case 1: [Link]("One"); break; case 5:
[Link]("Five"); break; default: [Link]("Other number"); break;
} } }

3. Swapping two numbers using Pass by Value


using System; class SwapByValue { static void Swap(int a, int b) { int temp = a; a =
b; b = temp; [Link]($"Inside Swap Method: a={a}, b={b}"); } static void
Main() { int x = 10, y = 20; [Link]($"Before Swap: x={x}, y={y}");
Swap(x, y); [Link]($"After Swap: x={x}, y={y}"); } }

4. Swapping two numbers using Pass by Reference


using System; class SwapByRef { static void Swap(ref int a, ref int b) { int temp =
a; a = b; b = temp; } static void Main() { int x = 10, y = 20;
[Link]($"Before Swap: x={x}, y={y}"); Swap(ref x, ref y);
[Link]($"After Swap: x={x}, y={y}"); } }

5. Type Casting in C#
using System; class TypeCasting { static void Main() { int i = 100; double d = i; //
Implicit casting [Link]("Implicit Casting: int to double = " + d); double
x = 123.45; int y = (int)x; // Explicit casting [Link]("Explicit Casting:
double to int = " + y); } }

6. Boxing and Unboxing in C#


using System; class BoxingUnboxing { static void Main() { int num = 100; object obj
= num; // Boxing int val = (int)obj; // Unboxing [Link]("Boxed value: " +
obj); [Link]("Unboxed value: " + val); } }

7. Inheritance in C#
using System; class Animal { public void Eat() { [Link]("Eating..."); } }
class Dog : Animal { public void Bark() { [Link]("Barking..."); } } class
TestInheritance { static void Main() { Dog d = new Dog(); [Link](); [Link](); } }

8. Interface in C#
using System; interface IAnimal { void Eat(); } class Dog : IAnimal { public void
Eat() { [Link]("Dog is eating."); } } class Program { static void Main()
{ Dog d = new Dog(); [Link](); } }

9. Sealed Class in C#
using System; sealed class A { public void Display() { [Link]("This is a
sealed class."); } } class Program { static void Main() { A obj = new A();
[Link](); } }

10. Abstract Class in C#


using System; abstract class Animal { public abstract void Sound(); } class Dog :
Animal { public override void Sound() { [Link]("Dog barks"); } } class
Program { static void Main() { Animal a = new Dog(); [Link](); } }

11. Partial Class in C#


using System; partial class Student { public void Name() { [Link]("Name:
John"); } } partial class Student { public void Age() { [Link]("Age:
21"); } } class Program { static void Main() { Student s = new Student(); [Link]();
[Link](); } }

12. Exception Handling in C#


using System; class ExceptionDemo { static void Main() { try { int a = 10, b = 0;
int c = a / b; } catch (DivideByZeroException e) { [Link]("Exception
caught: " + [Link]); } finally { [Link]("Finally block executed."); }
} }

You might also like