0% found this document useful (0 votes)
2 views

C# Notes

The document explains user-defined data types in C#, focusing on structures and access specifiers, as well as methods and constructors. It covers different types of constructors, inheritance, interfaces, abstract classes, and polymorphism, providing examples for clarity. Additionally, it highlights the limitations of inheritance in C# and the use of interfaces to achieve multiple inheritance.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

C# Notes

The document explains user-defined data types in C#, focusing on structures and access specifiers, as well as methods and constructors. It covers different types of constructors, inheritance, interfaces, abstract classes, and polymorphism, providing examples for clarity. Additionally, it highlights the limitations of inheritance in C# and the use of interfaces to achieve multiple inheritance.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

‭User Defined Datatypes:‬

‭ . To create a User Defined datatype or structure, we will need to use struct Keyword.‬
1
‭2. Under a structure, we can have data types and methods.‬
‭3. When we declare a variable inside a structure, it should use a proper access specifier.‬
‭4. Access specifier is used to expose the variable outside the class or structure.‬
‭5. There are 3 types of access specifier :‬
‭5.1 Public‬
‭5.2 Private‬
‭5.3 Protected‬
‭5.4 Internal‬
‭5.5 Protected Internal.‬
‭6.‬ ‭When‬ ‭we‬ ‭are‬ ‭declaring‬ ‭a‬ ‭variable‬ ‭without‬ ‭an‬ ‭access‬ ‭specifier,‬ ‭by‬ ‭default‬ ‭it‬ ‭will‬
‭consider it as a private access specifier.‬
‭7. We can explicitly define the access specifier as public.‬
‭8. Under the Main method, we need to declare user defined structure.‬
‭General _general;‬
‭9. We can use the structure, whatever the way we need.‬

‭Sample Program :‬

‭using System;‬

‭ amespace Sample_UserDefinedDatatype‬
n
‭{‬

i‭nternal class Program‬


‭{‬
‭public static void Main(string[] args)‬
‭{‬
‭General _general;‬

‭ onsole.WriteLine("Enter the first number");‬


C
‭_general._firstValue = Convert.ToInt32(Console.ReadLine());‬

/‭ / Convert is used to change from 1 datatype to another datatype.‬


‭// Console.Readline will be considering it as a string.‬
‭Console.WriteLine("Enter the second number");‬
‭_general._secondValue = Convert.ToInt32(Console.ReadLine());‬

‭_general._result = _general._firstValue + _general._secondValue;‬

‭ onsole.WriteLine("The resultant value is:" + " " + _general._result);‬


C
‭Console.ReadLine();‬
‭}‬
‭}‬

‭ truct General‬
s
‭{‬
‭public int _firstValue;‬
‭public int _secondValue;‬
‭public int _result;‬

‭}‬

‭class Add {‬

‭ rivate int _firstValue;‬


p
‭private int _secondValue;‬
‭private int _result;‬
‭}‬

‭}‬

‭Method:‬

I‭t‬ ‭is‬ ‭a‬ ‭block/set‬ ‭of‬ ‭code‬ ‭containers‬ ‭surrounded‬ ‭by‬ ‭a‬ ‭specific‬ ‭name,‬ ‭which‬ ‭is‬ ‭called‬ ‭a‬
‭Method.‬
‭Signature:‬

‭ ccess-specifier return_type datatype_name (Parameters)‬


A
‭{‬

‭}‬

‭Constructor:‬

‭ onstructor is a member method of a class‬


C
‭Constructor name is the same as the class name.‬

‭Types of Constructor:‬

‭ .‬
1 ‭ efault Constructor‬
D
‭2.‬ ‭Parameterized Constructor‬
‭3.‬ ‭Copy Constructor‬
‭4.‬ ‭Static Constructor‬
‭5.‬ ‭Private Constructor‬

‭ . Default Constructor:‬
1
‭Syntax: public ClassName() { }‬
‭Description:‬ ‭Default‬ ‭constructors‬ ‭are‬ ‭parameterless‬ ‭constructors‬ ‭provided‬ ‭by‬ ‭the‬
‭compiler‬ ‭if‬ ‭no‬ ‭constructor‬ ‭is‬ ‭defined‬ ‭in‬ ‭the‬ ‭class.‬ ‭They‬ ‭initialize‬ ‭fields‬ ‭to‬ ‭their‬ ‭default‬
‭values.‬
‭Example:‬

‭ ublic class Person‬


p
‭{‬
‭public string Name { get; set; }‬
‭public int Age { get; set; }‬

/‭ / Default Constructor‬
‭public Person()‬
‭{‬
‭Name = "John Doe";‬
‭Age = 30;‬
‭}‬
‭}‬

‭2. Parameterized Constructor‬

‭ yntax: public ClassName(type1 param1, type2 param2, ...) { }‬


S
‭Description:‬ ‭Parameterized‬ ‭constructors‬ ‭accept‬ ‭parameters‬ ‭to‬ ‭initialize‬ ‭an‬
‭object's state during object creation.‬

‭Example:‬

‭ ublic class Person‬


p
‭{‬
‭public string Name { get; set; }‬
‭public int Age { get; set; }‬

/‭ / Parameterized Constructor‬
‭public Person(string name, int age)‬
‭{‬
‭Name = name;‬
‭Age = age;‬
‭}‬
‭}‬

‭3. Copy Constructor‬

‭ yntax: public ClassName(ClassName obj) { }‬


S
‭Description:‬ ‭Copy‬ ‭constructors‬ ‭create‬ ‭a‬ ‭new‬ ‭object‬ ‭by‬ ‭copying‬ ‭the‬ ‭values‬ ‭of‬ ‭another‬
‭object of the same class.‬
‭Example:‬

‭ ublic class Person‬


p
‭{‬
‭public string Name { get; set; }‬
‭public int Age { get; set; }‬

/‭ / Copy Constructor‬
‭public Person(Person otherPerson)‬
‭{‬
‭ ame = otherPerson.Name;‬
N
‭Age = otherPerson.Age;‬
‭}‬
‭}‬

‭4. Static Constructor‬

‭ yntax: static ClassName() { }‬


S
‭Description:‬ ‭Static‬ ‭constructors‬ ‭are‬ ‭used‬‭to‬‭initialize‬‭static‬‭fields‬‭or‬‭perform‬‭any‬‭static‬
‭initialization‬ ‭before‬ ‭the‬ ‭class‬ ‭is‬ ‭used‬ ‭for‬ ‭the‬ ‭first‬ ‭time.‬ ‭They‬ ‭are‬ ‭called‬ ‭automatically‬
‭before the first instance is created or any static members are accessed.‬

‭Example:‬

‭using System;‬

‭ ublic class MyClass‬


p
‭{‬
‭private static readonly DateTime creationTime;‬

/‭ / Static constructor‬
‭static MyClass()‬
‭{‬
‭creationTime = DateTime.Now;‬
‭}‬

/‭ / Method to get creation time‬


‭public static DateTime GetCreationTime()‬
‭{‬
‭return creationTime;‬
‭}‬
‭}‬

‭ lass Program‬
c
‭{‬
‭static void Main()‬
‭{‬
‭// Access the creation time‬
‭DateTime time = MyClass.GetCreationTime();‬
‭Console.WriteLine($"Object created at: {time}");‬
‭}‬
‭}‬

‭5. Private Constructor‬

‭ yntax: private ClassName() { }‬


S
‭Description:‬ ‭Private‬ ‭constructors‬ ‭prevent‬ ‭the‬ ‭class‬ ‭from‬ ‭being‬ ‭instantiated‬ ‭by‬ ‭clients‬
‭and are often used in classes that contain only static members.‬
‭Example:‬

‭ ublic class Singleton‬


p
‭{‬
‭private static Singleton instance;‬

/‭ / Private Constructor‬
‭private Singleton() { }‬

‭ ublic static Singleton GetInstance()‬


p
‭{‬
‭if (instance == null)‬
‭{‬
‭instance = new Singleton();‬
‭}‬
‭return instance;‬
‭}‬
‭}‬

‭6. Destructor‬

‭ yntax: ~ClassName() { }‬
S
‭Description:‬ ‭Destructors‬ ‭are‬ ‭used‬ ‭to‬ ‭release‬ ‭resources‬ ‭held‬ ‭by‬ ‭an‬ ‭object‬ ‭before‬ ‭it‬ ‭is‬
‭destroyed‬ ‭by‬ ‭the‬ ‭garbage‬ ‭collector.‬ ‭They‬ ‭are‬ ‭less‬ ‭commonly‬ ‭used‬ ‭in‬ ‭C#‬ ‭due‬ ‭to‬ ‭the‬
‭presence of garbage collection.‬

‭Inheritance:‬
‭Inheritance is the process in which a child class inherits properties of the parent class.‬

‭Types of Inheritance:‬

‭ .‬ ‭Single Inheritance‬
1
‭2.‬ ‭Multilevel Inheritance‬
‭3.‬ ‭Multiple Inheritance‬
‭4.‬ ‭Hierarchical Inheritance‬
‭5.‬ ‭Hybrid Inheritance.‬
‭Note: C# allows only 2 types of inheritance, that is, single and multilevel inheritance.‬
‭Multiple‬ ‭inheritance‬ ‭is‬ ‭not‬ ‭allowed‬ ‭in‬ ‭C#,‬ ‭but‬ ‭could‬ ‭be‬ ‭achieved‬ ‭with‬ ‭the‬ ‭help‬ ‭of‬
‭interfaces.‬

‭1.‬ ‭Single inheritance:‬

‭ derived class inherits from only one base class.‬


A
‭Example:‬

‭ ublic class Animal‬


p
‭{‬
‭public void Eat()‬
‭{‬
‭Console.WriteLine("Animal is eating.");‬
‭}‬
‭}‬

‭ ublic class Dog : Animal‬


p
‭{‬
‭public void Bark()‬
‭{‬
‭Console.WriteLine("Dog is barking.");‬
‭}‬
‭}‬

‭2.‬ ‭Multilevel Inheritance‬

‭A derived class is used as a base class for another class.‬


‭Example:‬

‭public class Animal‬


‭{‬
‭public void Eat()‬
‭{‬
‭Console.WriteLine("Animal is eating.");‬
‭}‬
‭}‬

‭public class Dog : Animal‬


‭{‬
‭public void Bark()‬
‭{‬
‭Console.WriteLine("Dog is barking.");‬
‭}‬
‭}‬

‭3.‬ ‭Multiple Inheritance‬

I‭n‬ ‭C#‬ ‭Multiple‬ ‭inheritance‬ ‭can‬ ‭be‬ ‭achieved‬ ‭through‬ ‭interfaces.‬ ‭Interfaces‬ ‭in‬‭C#‬
‭provide‬ ‭a‬ ‭way‬ ‭to‬ ‭define‬ ‭contracts‬ ‭for‬ ‭classes‬ ‭without‬ ‭specifying‬ ‭the‬
‭implementation.‬ ‭A‬ ‭class‬‭can‬‭implement‬‭multiple‬‭interfaces,‬‭allowing‬‭it‬‭to‬‭inherit‬
‭behavior from multiple sources‬

‭Example:‬

‭using System;‬

/‭ / Interface for flying behavior‬


‭public interface IFlyable‬
‭{‬
‭void Fly();‬
‭}‬

/‭ / Interface for swimming behavior‬


‭public interface ISwimmable‬
‭{‬
‭void Swim();‬
‭}‬

/‭ / Class implementing both IFlyable and ISwimmable‬


‭public class Duck : IFlyable, ISwimmable‬
‭{‬
‭public void Fly()‬
‭{‬
‭Console.WriteLine("Duck is flying.");‬
‭}‬

‭ ublic void Swim()‬


p
‭{‬
‭Console.WriteLine("Duck is swimming.");‬
‭}‬
‭}‬

‭ lass Program‬
c
‭{‬
‭static void Main()‬
‭{‬
‭Duck duck = new Duck();‬
‭duck.Fly(); // Duck is flying‬
‭duck.Swim(); // Duck is swimming‬
‭}‬
‭}‬

‭Interfaces:‬

‭ n‬ ‭interface‬ ‭in‬ ‭C#‬ ‭is‬ ‭a‬ ‭reference‬ ‭type‬ ‭that‬ ‭defines‬ ‭a‬ ‭contract‬ ‭for‬ ‭what‬ ‭a‬ ‭class‬ ‭must‬
A
‭implement.‬ ‭It‬ ‭contains‬ ‭only‬‭the‬‭declaration‬‭of‬‭methods,‬‭properties,‬‭events,‬‭or‬‭indexers,‬
‭without providing any implementation.‬

‭Example:‬

‭ ublic interface IShape‬


p
‭{‬
‭double Area();‬
‭}‬

‭ ublic class Circle : IShape‬


p
‭{‬
‭public double Radius { get; set; }‬

‭ ublic double Area()‬


p
‭{‬
‭return Math.PI * Radius * Radius;‬
‭}‬
‭}‬

‭Abstract Class‬

‭ n‬ ‭abstract‬ ‭class‬ ‭in‬ ‭C#‬ ‭is‬ ‭a‬ ‭class‬ ‭that‬ ‭cannot‬ ‭be‬ ‭instantiated‬ ‭on‬ ‭its‬ ‭own‬ ‭and‬ ‭may‬
A
‭contain‬ ‭abstract‬ ‭methods,‬ ‭which‬ ‭are‬ ‭declared‬ ‭without‬ ‭an‬ ‭implementation.‬ ‭Abstract‬
‭classes can also contain concrete methods with implementations.‬

‭Example:‬

‭ ublic abstract class Shape‬


p
‭{‬
‭public abstract double Area();‬
‭}‬

‭ ublic class Circle : Shape‬


p
‭{‬
‭public double Radius { get; set; }‬

‭ ublic override double Area()‬


p
‭{‬
‭return Math.PI * Radius * Radius;‬
‭}‬
‭}‬
‭Polymorphism:‬

‭ olymorphism‬ ‭is‬ ‭the‬ ‭ability‬ ‭of‬ ‭an‬ ‭object‬ ‭to‬ ‭take‬ ‭on‬ ‭multiple‬ ‭forms.‬ ‭In‬ ‭OOP,‬ ‭it‬ ‭allows‬
P
‭objects of different classes to be treated as objects of a common superclass.‬

‭Types Of Polymorphism:‬

‭1.‬ C ‭ ompile-Time‬ ‭Polymorphism‬ ‭(Static‬ ‭Binding):‬ ‭This‬ ‭is‬ ‭achieved‬ ‭through‬ ‭method‬
‭overloading and operator overloading.‬
‭2.‬ ‭Run-Time‬ ‭Polymorphism‬ ‭(Dynamic‬ ‭Binding):‬ ‭This‬ ‭is‬ ‭achieved‬ ‭through‬ ‭method‬
‭overriding.‬

‭Example:‬

‭ ublic class Animal‬


p
‭{‬
‭public virtual void MakeSound()‬
‭{‬
‭Console.WriteLine("Animal makes a sound");‬
‭}‬
‭}‬

‭ ublic class Dog : Animal‬


p
‭{‬
‭public override void MakeSound()‬
‭{‬
‭Console.WriteLine("Dog barks");‬
‭}‬
‭}‬

‭ nimal animal = new Dog();‬


A
‭animal.MakeSound(); // Output: Dog barks‬

‭Method Overloading:‬

‭ ethod‬ ‭overloading‬ ‭allows‬ ‭a‬ ‭class‬ ‭to‬ ‭have‬ ‭multiple‬‭methods‬‭with‬‭the‬‭same‬‭name‬‭but‬


M
‭different parameters.‬
‭ ublic class Calculator‬
p
‭{‬
‭public int Add(int a, int b)‬
‭{‬
‭return a + b;‬
‭}‬

‭ ublic double Add(double a, double b)‬


p
‭{‬
‭return a + b;‬
‭}‬
‭}‬

‭Method Overriding:‬

‭ ethod‬ ‭overriding‬‭allows‬‭a‬‭subclass‬‭to‬‭provide‬‭a‬‭specific‬‭implementation‬‭of‬‭a‬‭method‬
M
‭that is already provided by its superclass.‬

‭using System;‬

‭ ublic class Animal‬


p
‭{‬
‭public virtual void MakeSound()‬
‭{‬
‭Console.WriteLine("Animal makes a sound");‬
‭}‬
‭}‬

‭ ublic class Dog : Animal‬


p
‭{‬
‭public override void MakeSound()‬
‭{‬
‭Console.WriteLine("Dog barks");‬
‭}‬
‭}‬

‭ lass Program‬
c
‭{‬
‭ tatic void Main()‬
s
‭{‬
‭Animal animal = new Dog();‬
‭animal.MakeSound(); // Output: Dog barks‬
‭}‬
‭}‬

‭Method Hiding:‬

‭ ethod‬ ‭hiding‬ ‭allows‬ ‭a‬ ‭subclass‬‭to‬‭provide‬‭a‬‭new‬‭implementation‬‭of‬‭a‬‭method‬‭that‬‭is‬


M
‭already provided by its superclass, effectively hiding the superclass's implementation.‬

‭ ublic class BaseClass‬


p
‭{‬
‭public void Display()‬
‭{‬
‭Console.WriteLine("BaseClass display");‬
‭}‬
‭}‬

‭ ublic class DerivedClass : BaseClass‬


p
‭{‬
‭public new void Display()‬
‭{‬
‭Console.WriteLine("DerivedClass display");‬
‭}‬
‭}‬

‭ aseClass obj1 = new BaseClass();‬


B
‭obj1.Display(); // Output: BaseClass display‬

‭ aseClass obj2 = new DerivedClass();‬


B
‭obj2.Display(); // Output: BaseClass display (Method hiding)‬

‭Exception Handling:‬
‭ xception‬ ‭handling‬ ‭is‬ ‭a‬ ‭mechanism‬ ‭in‬ ‭programming‬ ‭that‬ ‭allows‬ ‭you‬ ‭to‬ ‭deal‬ ‭with‬
E
‭unexpected‬‭or‬‭exceptional‬‭situations‬‭that‬‭may‬‭occur‬‭during‬‭the‬‭execution‬‭of‬‭a‬‭program.‬
‭It helps in gracefully handling errors and prevents the program from crashing.‬

I‭n‬ ‭C#exception‬ ‭handling‬ ‭is‬ ‭done‬ ‭using‬ ‭̀try`,‬ ‭̀catch`,‬ ‭̀finally`,‬ ‭and‬ ‭̀throw`‬ ‭keywords.‬
‭Here's a basic example:‬

‭using System;‬

‭ lass Program‬
c
‭{‬
‭static void Main()‬
‭{‬
‭try‬
‭{‬
‭int[] numbers = { 1, 2, 3 };‬
‭Console.WriteLine(numbers[5]); // Trying to access an index out of bounds‬
‭}‬
‭catch (Exception ex)‬
‭{‬
‭Console.WriteLine("An error occurred: " + ex.Message);‬
‭}‬
‭finally‬
‭{‬
‭Console.WriteLine("Finally‬‭block‬‭always‬‭executes,‬‭whether‬‭an‬‭exception‬‭occurs‬
‭or not.");‬
‭}‬

‭Console.WriteLine("Program continues after the exception handling block.");‬


‭}‬
‭}‬

I‭n‬ ‭this‬ ‭example,‬ ‭the‬ ‭̀try`‬ ‭block‬ ‭contains‬ ‭code‬ ‭that‬ ‭might‬ ‭throw‬‭an‬‭exception‬‭(trying‬‭to‬
‭access‬ ‭an‬ ‭index‬ ‭out‬ ‭of‬ ‭bounds‬ ‭in‬ ‭an‬ ‭array).‬ ‭If‬ ‭an‬ ‭exception‬ ‭occurs,‬ ‭the‬ ‭̀catch`‬ ‭block‬
‭catches‬ ‭the‬ ‭exception,‬ ‭and‬ ‭you‬ ‭can‬ ‭handle‬ ‭it‬ ‭(printing‬ ‭an‬ ‭error‬‭message‬‭in‬‭this‬‭case).‬
‭The‬ ‭̀finally`‬ ‭block‬ ‭is‬ ‭optional‬ ‭and‬ ‭is‬ ‭used‬ ‭to‬‭execute‬‭cleanup‬‭code‬‭that‬‭should‬‭always‬
‭run, whether an exception occurs or not.‬

You might also like