0% found this document useful (0 votes)
4 views23 pages

Dotnet

The document provides an overview of C#, a modern programming language by Microsoft, detailing its features such as object-oriented programming, type safety, and cross-platform capabilities. It includes various C# programming examples demonstrating concepts like prime number checking, properties, constructors, inheritance, interfaces, and more. Each section contains code snippets and expected outputs to illustrate the functionality of different C# features.

Uploaded by

joyboy4833
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views23 pages

Dotnet

The document provides an overview of C#, a modern programming language by Microsoft, detailing its features such as object-oriented programming, type safety, and cross-platform capabilities. It includes various C# programming examples demonstrating concepts like prime number checking, properties, constructors, inheritance, interfaces, and more. Each section contains code snippets and expected outputs to illustrate the functionality of different C# features.

Uploaded by

joyboy4833
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Contents

1. What is C#?.....................................................2
1.1. features of C#...............................................2
2. Write a C# program to find and display all prime numbers between
two numbers entered by the user......................................2
3. Write a C# program to demonstrate get and set properties........4
4. Write a C# program to demonstrate default and parameterized
constructors.........................................................4
5. Write a C# program to demonstrate abstract class and methods.. . .5
6. Write a C# program to demonstrate destructor....................7
7. Write a C# program to demonstrate static members................8
8. Write a C# program to demonstrate dynamic binding...............9
9. Write a C# program to demonstrate inheritance..................10
10. Write a C# program to demonstrate interfaces...................10
11. Write a C# program to demonstrate enum.........................11
12. Write a C# program to demonstrate generics.....................12
13. Write a C# program to demonstrate method overriding............13
14. Write a C# program to demonstrate method overloading...........14
15. Write a C# program to demonstrate base keyword.................15
16. Write a C# program to demonstrate array, list, and dictionary.. 15
17. Write a C# program to demonstrate LINQ.........................16
18. Lambda Expressions Example and Types...........................17
19. Write a C# program to demonstrate SQL database connection.. . . . .18
20. Write a C# program to demonstrate delegate.....................19
21. Write a C# program to demonstrate event........................20
1. What is C#?
C# is a modern programming language by Microsoft, used for building apps, games, and websites. It
is easy to read, type-safe, and runs on the .NET framework.

1.1. features of C#

 Object-Oriented: Supports classes, objects, inheritance, and polymorphism.

 Type-Safe: Prevents type errors and enhances program reliability.

 Simple & Modern: Easy to read and write, with modern language features.
 Rich Library Support: Comes with a large set of built-in libraries in .NET.

 Cross-Platform: Can run on Windows, Linux, and macOS using .NET Core/.NET
5+.

 Automatic Memory Management: Uses garbage collection to manage memory.

 Interoperability: Can work with other languages and technologies easily.

2. Write a C# program to find and display all prime numbers between two
numbers entered by the user.
using System;

class PrimeCheck
{
static void Main()
{
[Link]("Enter a number: ");

int num = Convert.ToInt32([Link]());

bool isPrime = true;


if (num <= 1)
{
isPrime = false;
}
else
{
for (int i = 2; i < num; i++)
{
if (num % i == 0)
{
isPrime = false;
break;
}
}
}

if (isPrime)
[Link](num + " is a Prime Number.");
else
[Link](num + " is NOT a Prime Number.");
}
}

OUTPUT:
3. Write a C# program to demonstrate get and set properties.
using System;

class Person
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
}
class Program
{
static void Main()
{
Person p = new Person();
[Link] = "Ujwal";
[Link]("Name is: " + [Link]);
}
}
Output:
4. Write a C# program to demonstrate default and parameterized constructors.
using System;

class Person
{
public string Name;

public Person() // Default constructor


{
Name = "Unknown";
}

public Person(string name) // Parameterized constructor


{
Name = name;
}
}

class Program
{
static void Main()
{
Person p1 = new Person();
Person p2 = new Person("Ujwal");

[Link]("Default Constructor: " + [Link]);


[Link]("Parameterized Constructor: " + [Link]);
}
}

Output:
5. Write a C# program to demonstrate abstract class and methods.
using System;
abstract class Animal
{
public abstract void Speak(); // Abstract method
}

class Dog : Animal


{
public override void Speak()
{
[Link]("Dog barks");
}
}

class Program
{
static void Main()
{
Dog d = new Dog();
[Link]();
}
}
Output:

6. Write a C# program to demonstrate destructor.


using System;

class Person
{
~Person()
{
[Link]("Destructor called, object destroyed.");
}
}

class Program
{
static void Main()
{
Person p = new Person();
[Link]("Program ends.");
}
}

Output:

7. Write a C# program to demonstrate static members.


using System;
class MathOperations
{
public static int Add(int a, int b)
{
return a + b;
}
}

class Program
{
static void Main()
{
[Link]("Sum: " + [Link](5, 3));
}
}
Output:
8. Write a C# program to demonstrate dynamic binding.
using System;

class Animal
{
public virtual void Speak()
{
[Link]("Animal speaks");
}
}

class Dog : Animal


{
public override void Speak()
{
[Link]("Dog barks");
}
}

class Program
{
static void Main()
{
Animal a = new Dog(); // Dynamic binding
[Link](); // Calls Dog's Speak at runtime
}
}
output:
9. Write a C# program to demonstrate inheritance.
using System;
class Animal
{
public void Eat()
{
[Link]("Eating...");
}
}
class Dog : Animal
{
public void Bark()
{
[Link]("Barking...");
}
}
class Program
{
static void Main()
{
Dog d = new Dog();
[Link](); // Inherited
[Link]();
}
}

Output:
10. Write a C# program to demonstrate interfaces.
using System;
interface IAnimal
{
void Speak();
}

class Dog : IAnimal


{
public void Speak()
{
[Link]("Dog barks");
}
}
class Program
{
static void Main()
{
IAnimal a = new Dog();
[Link]();
}
}
output:
11. Write a C# program to demonstrate enum.
using System;

enum Days { Sun, Mon, Tue, Wed, Thu, Fri, Sat }

class Program
{
static void Main()
{
Days today = [Link];

[Link]("Today is: " + today);


}
}

Output:
12. Write a C# program to demonstrate generics.

using System;
using [Link];

class Program
{
static void Main()
{
List<int> numbers = new List<int>();
[Link](10);
[Link](20);

foreach (int n in numbers)


{
[Link](n);
}
}
}

Output:
13. Write a C# program to demonstrate method overriding.
using System;

class Animal
{
public virtual void Speak()
{
[Link]("Animal speaks");
}
}

class Cat : Animal


{
public override void Speak()
{
[Link]("Cat meows");
}
}

class Program
{
static void Main()
{
Animal a = new Cat();
[Link](); // Calls overridden method
}
}
Output:
14. Write a C# program to demonstrate method overloading.
using System;
class MathOperations
{
public int Add(int a, int b)
{
return a + b;
}

public double Add(double a, double b)


{
return a + b;
}
}
class Program
{
static void Main()
{
MathOperations m = new MathOperations();
[Link]([Link](2, 3));
[Link]([Link](2.5, 3.5));
}
}
Output:
15. Write a C# program to demonstrate base keyword.
using System;

class Person
{
public void Speak()
{
[Link]("Person speaks");
}
}

class Man : Person


{
public void Show()
{
[Link](); // Access parent method
}
}

class Program
{
static void Main()
{
Man m = new Man(); // Use derived class object
[Link](); // Calls base class Speak()
}
}
Output:
16. Write a C# program to demonstrate array, list, and dictionary.

using System;

using [Link];

class Program
{
static void Main()
{
int[] arr = { 1, 2, 3 };

List<int> list = new List<int> { 4, 5, 6 };

Dictionary<int, string> dict = new Dictionary<int, string> {


{ 1, "A" }, { 2, "B" } };

[Link]("Array: " + arr[0]);

[Link]("List: " + list[1]);

[Link]("Dictionary: " + dict[2]);


}
}

Output:
17. Write a C# program to demonstrate LINQ.

using System;

using [Link];

class Program
{
static void Main()
{
int[] numbers = { 1, 2, 3, 4, 5 };

var evens = from n in numbers

where n % 2 == 0

select n;

foreach (var n in evens)


{
[Link](n);
}
}
}

Output:
18. Lambda Expressions Example and Types
using System;
class Program
{
static void Main()
{
Func<int, int> square = x => x * x; // Expression lambda

Action<int> print = x => { [Link](x); }; //


Statement lambda
[Link](square(5));

print(10);
}
}

Output:
19. Write a C# program to demonstrate SQL database connection.
using System;
using [Link];

class Program
{
static void Main()
{
string connStr = "Data Source=.;Initial Catalog=TestDB;Integrated
Security=True";
SqlConnection con = new SqlConnection(connStr);

try
{
[Link]();
[Link]("Database Connected");
}
catch (Exception ex)
{
[Link]("Error: " + [Link]);
}
finally
{
[Link]();
}
}
}
20. Write a C# program to demonstrate delegate.
using System;

delegate void Print(int n);

class Program
{
static void Show(int n)
{
[Link]("Number: " + n);
}

static void Main()


{
Print p = Show;
p(5);
}
}
21. Write a C# program to demonstrate event.
using System;

class Button
{
public event Action Click;

public void OnClick()


{
Click?.Invoke();
}
}

class Program
{
static void Main()
{
Button b = new Button();
[Link] += () => [Link]("Button clicked!");
[Link]();
}
}

Output:

You might also like