Unit 1
Unit 1
NET Core - I
#2101CS511
Unit-1
C# (C-Sharp)
Fundamentals
namespace HelloWorld
Here, namespace HelloWorld is the main namespace of our application, and by default, our application classes will be a part
of it.
class Program
Here, class Program is used to define a class (Program) in the namespace (Helloworld).
The class (Program) will contain all the variables, methods, etc., and we can define more than one class in same namespace
based on our requirements.
static void Main(string[] args)
The keyword static tells us that the main method can be accessible without instantiating the class (Program).
Another keyword void tells us that what this method should return.
The name Main will refer to the name of our class method (Program). The Main() method is the entry point of our console
application.
Here, Console.WriteLine() and Console.ReadLine() methods are used to write a text to the console and read the input from
the console.
#2101CS511 (ASP.NET Core - I) Unit 1 – C#
Prof. Naimish R. Vadodariya 15
Compile and Run C# Hello World Program
To see Program's output, you need to compile and run the application by pressing either Ctrl
+ F5 or click on the Start option in the menu bar like as shown below.
Once you click on the Start option or Ctrl + F5, our program will get compiled and show the
result as shown below.
In the above example, + operator adds two number literals and assign the result
to a variable.
It also adds the values of two int variables and assigns the result to a variable.
Some operators behave differently based on the type of the operands. For
example,
1 string +greet1
operator concatenates
= "Hello two strings.
" + "World!";
Example Output
1 using System;
2 namespace MyFirstApp
3 {
4 class Program
5 {
6 public static void Main(string[] args)
7 {
8 bool a = true;
9 bool b = false;
10 Console.WriteLine(a && b);
11 Console.WriteLine(a || b);
12 Console.WriteLine(!(a && b));
13 Console.ReadLine();
14 }
15 }
16 }
We defined a class “users” using class keyword with public access modifier.
Here, the public access specifier will allow the users to create objects for this class, and
inside of the body class, we can create required fields, properties, methods, and events for
specific usage.
namespace MyFirstApp
{
class Program
{
static void Main(string[]
args)
{
ArrayList A1 = new
ArrayList();
A1.Add(10);
A1.Add("Hello");
A1.Add(true);
Console.WriteLine(A1[0]);
Console.WriteLine(A1[1]);
Console.WriteLine(A1[2]);
Console.ReadLine();
}
}
} Prof. Naimish R. Vadodariya
#2101CS511 (ASP.NET Core - I) Unit 1 – C#
54
Example – ArrayList Class
using System;
using System.Collections;
Output :
namespace MyFirstApp
class Program
Console.WriteLine(A1.Count);
Console.WriteLine(A1.Contains(10));
Console.WriteLine("-------------------");
Console.WriteLine(A1[1]);
A1.RemoveAt(1);
Console.WriteLine(A1[1]);
Console.ReadLine();
namespace MyFirstApp
{
class Program
{
public static void Main(string[] args)
{
Stack st = new Stack();
st.Push(1);
st.Push(2);
st.Push(3);
namespace MyFirstApp
class Program
st.Push(1);
st.Push(2);
st.Push(3);
st.Pop();
Console.WriteLine(obj);
Console.ReadLine();
using System.Collections;
Output :
namespace MyFirstApp
class Program
qt.Enqueue(1);
qt.Enqueue(2);
qt.Enqueue(3);
qt.Dequeue();
Console.WriteLine(obj);
Console.ReadLine();
using System.Collections;
Output :
namespace MyFirstApp
class Program
ht.Add("1", "Darshan");
ht.Add("2", "Institute");
Console.WriteLine(ht[k]);
Console.ReadLine();
numberNames.Add(3, "Three"); }
numberNames.Add(4, null);
numberNames.Add(10, "Ten");
numberNames.Add(5, "Five");
using System.Collections;
Output :
namespace MyFirstApp
class Program
ba[0] = true;
ba[1] = false;
ba[2] = true;
ba[3] = false;
Console.WriteLine(obj);
Console.ReadLine();
Single Inheritance
Multi-level Inheritance
Multiple Inheritance
Multipath Inheritance
Hierarchical Inheritance
Hybrid Inheritance
#2101CS511 (ASP.NET Core - I) Unit 1 – C#
Prof. Naimish R. Vadodariya 71
Single Inheritance
Class A (Base)
Single Inheritance
In single inheritance, a
derived class is created from a
single base class.
Class B (Derived)
class Program
{
static void Main(string[] args)
{
Teacher d = new Teacher();
d.Teach();
Student s = new Student();
s.Learn();
s.Teach();
Console.ReadKey();
}
}
Class A (Base)
Multi-level
Inheritance
In Multi-level inheritance, a
Class B derived class is created from
another derived class.
Class C
//Base,Derived Class
//Base Class
class Student : Teacher
class Teacher
{
{
public void Learn()
public void Teach()
{
{
Console.WriteLine("Teach");
Console.WriteLine("Learn");
}
}
}
}
class Program
{
static void Main(string[] args)
//Derived Class {
Teacher d = new Teacher();
class SemFour : Student d.Teach();
{
Student s = new Student();
public void FourthSem() s.Learn();
{ s.Teach();
Class C
Multiple Inheritance
In Multiple inheritance, a derived class is created from more than
one base class.
This type of inheritance is not supported by .NET Languages
like C#, F# etc.
#2101CS511 (ASP.NET Core - I) Unit 1 – C#
Prof. Naimish R. Vadodariya 76
Example – Multiple Inheritance
//Derived Class
class SemFour : Student , Teacher
{
public void FourthSem()
{
Console.WriteLine("Semester
Four Students");
}
}
Class A
Class B Class C
Class D
Multipath Inheritance
In Multipath inheritance, a derived class is created from another
derived classes and the same base class of another derived classes.
This type of inheritance is not supported by .NET Languages
like C#, F# etc.
#2101CS511 (ASP.NET Core - I) Unit 1 – C#
Prof. Naimish R. Vadodariya 78
Example – Multipath Inheritance
Class A
Class C Class B
Class A Class F
Class C Class B
Class D Class E
Functions Description
// Make String Clone
Clone() Console.WriteLine(str1.Clone());
//Compare two string value and returns 0 for true and 1 for false
CompareTo() Console.WriteLine(str1.CompareTo(lastname));
//Check whether specified value exists or not in string
Contains() Console.WriteLine(str1.Contains("ven"));
//Check whether specified value is the last character of string
EndsWith() Console.WriteLine(str1.EndsWith("n"));
//Compare two string and returns true and false
Equals() Console.WriteLine(str1.Equals(lastname));
//Returns the first index position of specified value the first index
IndexOf() position of specified value
Console.WriteLine(str1.IndexOf("e"));
Functions Description
//Covert string into lower case
ToLower() Console.WriteLine(str1.ToLower());
//Convert string into Upper case
ToUpper() Console.WriteLine(str1.ToUpper());
//Insert substring into string
Insert() Console.WriteLine(str1.Insert(0, "Hello"));
//Returns the last index position of specified value
LastIndexOf() Console.WriteLine(str1.LastIndexOf("e"));
//Returns the Length of String
Length Console.WriteLine(str1.Length);
//Deletes all the characters from begining to specified index.
Remove() Console.WriteLine(str1.Remove(5));
// Replace the character
Replace() Console.WriteLine(str1.Replace('e', 'i'));
Functions Description
//Check wheater first character of string is same as specified value
StartsWith() Console.WriteLine(str1.StartsWith("S"));
//Returns substring
Substring() Console.WriteLine(str1.Substring(2, 5));
//It removes starting and ending white spaces from string.
Trim() Console.WriteLine(str1.Trim());
class Program
int number = 0;
int div = 0;
try
catch (DivideByZeroException)
Console.WriteLine("Exception Occured");
Console.ReadLine();
try Output :
{
catch (Exception e)
Accessibility:
Can be accessed by objects of the class
Can be accessed by derived classes
Accessibility:
Cannot be accessed by object
Cannot be accessed by derived classes
Accessibility:
Cannot be accessed by object
By derived classes
Accessibility:
The variable or classes that are declared with internal can be access by any member within application.
It is the default access specifiers for a class in C# programming.
Accessibility:
Within the class in which they are declared
Within the derived classes of that class available within the same assembly
Outside the class within the same assembly
Within the derived classes of that class available outside the assembly
C# Types Description
enum The default and only access modifier supported is public.
Class The default access for a class is private.
It may be explicitly defined using any of the access modifiers.
Interface and enumeration members are always public and no access modifiers are allowed.
Properties Indexers
Properties don't require this keyword Indexers are created with this keyword
Properties are accessed by their names Indexers are accessed using indexes
Properties are also known as the smart fields Indexers are also known as smart arrays
A get accessor of a property has no parameters & A Indexers in C# must have atleast one parameter &
set accessor of a property contains the implicit value it also supports more than one different types of
parameter. parameters
Thank
You