KEMBAR78
Dotnet Practical 1 | PDF | C Sharp (Programming Language) | Computer Programming
0% found this document useful (0 votes)
19 views15 pages

Dotnet Practical 1

The document provides a comprehensive overview of C# programming concepts, including examples of data types, operators, conditional statements, loops, arrays, strings, and object-oriented programming principles such as classes, encapsulation, inheritance, and constructors. Each section includes code snippets demonstrating the concepts in action. The document serves as a practical guide for understanding and applying C# in .NET Framework.

Uploaded by

burhanasikwala
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)
19 views15 pages

Dotnet Practical 1

The document provides a comprehensive overview of C# programming concepts, including examples of data types, operators, conditional statements, loops, arrays, strings, and object-oriented programming principles such as classes, encapsulation, inheritance, and constructors. Each section includes code snippets demonstrating the concepts in action. The document serves as a practical guide for understanding and applying C# in .NET Framework.

Uploaded by

burhanasikwala
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
You are on page 1/ 15

DOTNET PRACTICAL 1

Name- Akriti Rajbhar Date- 17 July 2025


Roll no- KCSYCS45

 Simple example of C#:


using System;
class
Program
{
static void Main()
{
Console.WriteLine("Welcome to .NET Framework !");
}
}

 Example of Data types:


// See https://aka.ms/new-console-template for more information
using System;
using System.Runtime.CompilerServices;
class
Program
{
static void Main()
{
int rollNo = 101;
string studentName = "Akriti";
float percentage = 85.5f;
char grade = 'A';
bool isPassed = true;

Console.WriteLine("Student info:");
Console.WriteLine("Roll no:" +rollNo);
Console.WriteLine("Name:" +studentName);
Console.WriteLine("Percentage: "+percentage);
Console.WriteLine("Grade:" +grade);
Console.WriteLine("Passed:" +isPassed);
}
}

1
 Example of types of operators in C#:
// See https://aka.ms/new-console-template for more information
using System;
using System.Runtime.CompilerServices;
class
Program
{
static void Main()
{
int a = 10, b = 3;
Console.WriteLine("Arithmetic Operators:");
Console.WriteLine("a+b=" +(a+b));
Console.WriteLine("a-b=" + (a - b));
Console.WriteLine("a*b=" + (a * b));
Console.WriteLine("a/b=" + (a / b));
Console.WriteLine("a%b=" + (a % b));
Console.WriteLine("lnReational Operators:");
Console.WriteLine("a==b=" + (a == b));
Console.WriteLine("a>b=" + (a > b));
Console.WriteLine("lnLogical Operators:");
Console.WriteLine("a>5 && b<5 =" + (a > 5 && b < 5));
Console.WriteLine("lnAssignment Operators:");
Console.WriteLine("a after a+=5 =" +a);
}
}

2
 Types of conditional statements:-
1. If statement:
// See https://aka.ms/new-console-template for more information
using System;
using System.Runtime.CompilerServices;
class
Program
{
static void Main()
{
int num = 10;
if (num>5)
{
Console.WriteLine("Number is greatert than 5");
}
}
}

2. If Else statement:
// See https://aka.ms/new-console-template for more information
using System;
using System.Runtime.CompilerServices;
class
Program
{
static void Main()
{
int age = 16;
if (age>=18)
{
Console.WriteLine("You can vote");
}
else
{
Console.WriteLine("You cannot vote");
}
}
}

3
3. Else If statement:
// See https://aka.ms/new-console-template for more information
using System;
using System.Runtime.CompilerServices;
class
Program
{
static void Main()
{
int marks = 75;
if (marks >= 90)
{
Console.WriteLine("A");
}
else if (marks >= 60)
{
Console.WriteLine("B");
}
else
{
Console.WriteLine("C")
}
}
}

4. Switch statement:
// See https://aka.ms/new-console-template for more information
using System;
using System.Runtime.CompilerServices;
class
Program
{
static void Main()
{
int day = 3;
switch(day)
{
case 1:
Console.WriteLine("Monday");
break;
case 2:
Console.WriteLine("Tuesday");
break;
case 3:
Console.WriteLine("Wednesday");
break;
case 4:
Console.WriteLine("Thursday");
break;
case 5:
Console.WriteLine("Friday");

4
break;
default:
Console.WriteLine("Invalid day");
break;
}
}
}

5. Ternary Operator :
using System;
class Program
{
static void Main()
{
int age = 18;
string message = (age >= 18) ? "You can vote." : "You cannot vote.";
Console.WriteLine(message);
}
}

 Loops :
1. For Loop:
for(int i=0; i < 5; i++)
Console.WriteLine(i);

5
2. While Loop:
int i = 0;
while (i < 5)
{
Console.WriteLine(i);
i++;
}

3. Do-While Loop:
int i = 0;
do
{
Console.WriteLine(i);
i++;
} while (i < 5);

C# - for vs foreach Loop


 For Loop Example:
string[] fruits = { "Apple", "Mango", "Banana" };
for (int i = 0; i < fruits.Length; i++)
{
Console.WriteLine("Fruit at index " + i + ": " + fruits[i]);
}

6
 ForEach Loop Example:
using System;
using System.Runtime.CompilerServices;
class
Program
{
static void Main()
{
string[] fruits = { "Apple", "Mango", "Banana" };
foreach (string fruit in fruits)
{
Console.WriteLine("Fruits : " + fruit);
}
}
}

 Jump statement:
1. For Break statement:
// See https://aka.ms/new-console-template for more information
using System;
using System.Runtime.CompilerServices;
class
Program
{
static void Main()
{
for (int i=0;i<5;i++)
{
if (i == 3)
break;
Console.WriteLine(i);
}
}
}

7
2. For Continue statement:
// See https://aka.ms/new-console-template for more information
using System;
using System.Runtime.CompilerServices;
class
Program
{
static void Main()
{
for (int i = 1; i <= 5; i++)
{
if (i == 3)
continue;
Console.WriteLine(i);
}
}
}

3. Return statement:
// See https://aka.ms/new-console-template for more information
using System;
using System.Runtime.CompilerServices;
class
Program
{
static void ShowMessage()
{
Console.WriteLine("Start");
return;
Console.WriteLine("This won't be printed");
}
static void Main()
{
ShowMessage();

8
}
}

 Types of array statement:


1. Single Dimensional:
// See https://aka.ms/new-console-template for more information
using System;
using System.Runtime.CompilerServices;
class
Program
{
static void Main()
{
int[] numbers = { 1, 2, 3 };
Console.WriteLine(numbers[0]);
}
}

2. Multi Dimensional:
// See https://aka.ms/new-console-template for more information
using System;
using System.Runtime.CompilerServices;
class
Program
{
static void Main()
{
int[,] matrix = { { 1, 2 }, { 3, 4 } };

}
}

3. Jagged Array:
// See https://aka.ms/new-console-template for more information
using System;
using System.Runtime.CompilerServices;
class
Program

9
{
static void Main()
{
int[][] jaggedArray;
jaggedArray = new int[3][];
jaggedArray[0]=new int[2];
jaggedArray[1] = new int[3];
jaggedArray[2] = new int[1];
Console.WriteLine();
}
}

 Strings and String Methods


1. Basic Example of String:
// See https://aka.ms/new-console-template for more information
using System;
using System.Runtime.CompilerServices;
class
Program
{
static void Main()
{
string message = "WElcome to C#";
Console.WriteLine(message);

}
}

2. String Declaration and Initialization:


// See https://aka.ms/new-console-template for more information
using System;
using System.Runtime.CompilerServices;
class
Program
{
static void Main()
{
string str1 = "Hello";
string str2 = new string("World");
Console.WriteLine(str1 + str2);

10
}
}

 Full String Example:


class Program
{
static void Main()
{
string text = " Welcome to C# ";

Console.WriteLine("Original: " + text);


Console.WriteLine("Trimmed: " + text.Trim());
Console.WriteLine("Uppercase: " + text.ToUpper());
Console.WriteLine("Contains 'C#': " + text.Contains("C#"));
Console.WriteLine("Substring(3,5): " + text.Substring(3, 5));
}
}

 Objects – Oriented programming concept:-


1. Classes and objects:
// See https://aka.ms/new-console-template for more information
using System;
using System.Runtime.CompilerServices;

public class Car


{
public string Brand;
public void start()
{
Console.WriteLine("Car is starting");
}
}
class Program
{

11
static void Main()
{
Car myCar = new Car();
myCar.Brand = " Toyota";
Console.WriteLine("Brand" + myCar.Brand);
myCar.start();
}
}

2. Encapsulation and Abstraction :


// See https://aka.ms/new-console-template for more information
using System;
using System.Runtime.CompilerServices;
public class BankAccount
{
private double balance = 100;
public void Deposit(double amount)
{
if (amount < 0)
balance += amount;
}
public double GetBalance()
{
return balance;
}
}
class Program
{
static void Main()
{
BankAccount ac = new BankAccount();
ac.Deposit(500);
Console.WriteLine("Balance :" + ac.GetBalance());
}
}

3. Inheritance :

12
// See https://aka.ms/new-console-template for more information
using System;
using System.Runtime.CompilerServices;
class Animal
{
public void Eat()
{
Console.WriteLine("Animal is eating");
}
class Dog : Animal
{
public void Bark()
{
Console.WriteLine("Dog is barking");
}

}
class Program
{
static void Main()
{
Dog dog = new Dog();
dog.Eat();
dog.Bark();
}
}
}

4. Constructor :
// See https://aka.ms/new-console-template for more information
using System;
using System.Runtime.CompilerServices;
public class Student
{
public string Name;
public Student(string name)
{
Name = name;
}
class Program
{
static void Main()
{
Student s = new Student("Ravi");
Console.WriteLine(s.Name);
}
}
}

13
5. .net core console app:

using System;

class Car
{
public string Model;

public Car(string model)


{
Model = model;
}

public void Drive()


{
Console.WriteLine($"{Model} is driving");
}
}

class Program
{
static void Main(string[] args)
{
Car c1 = new Car("Honda");
c1.Drive();
}
}

14
15

You might also like