BCA / B.Tech 11 min read

Type Safety in Hindi

Type Safety using the Cast Operator in C# in Hindi 


  • C# में type safety का मतलब होता है कि आप केवल सही प्रकार (type) के डेटा को ही किसी variable में store कर सकते हैं, जिससे रनटाइम errors को minimize किया जा सके। C# में type safety सुनिश्चित करने के लिए cast operators का इस्तेमाल किया जाता है। 
  • C# में type safety का मतलब है कि आप केवल उन types को ही assign या cast कर सकते हैं जो logically सही हैं। Value types और Reference types के बीच अंतर यह है कि value types के पास अपनी values होती हैं, जबकि reference types के पास object की memory location होती है।
  •  Cast operator आपको types के बीच conversion करने में मदद करता है, लेकिन आपको यह सुनिश्चित करने की जरूरत है कि आपके casting operations type-safe हों, ताकि runtime errors से बचा जा सके।
Cast operator दो प्रकार के होते हैं:

  • Implicit Cast (Automatic Cast): जब आप एक प्रकार को दूसरे प्रकार में बिना किसी explicit type conversion के convert करते हैं।
  • Explicit Cast (Manual Cast): जब आप किसी object या value को एक प्रकार से दूसरे प्रकार में बदलते हैं और यह C# compiler को बताना होता है कि आप जानबूझकर ऐसा कर रहे हैं।
  • Cast Operator का उपयोग करने के लिए आपको प्रकारों के बीच compatibility की जांच करनी होती है, और जब तक types compatible न हों, तब तक explicit cast operator को इस्तेमाल किया जाता है।

Type Safety in Hindi

References Types और Value Types के बीच अंतर :

C# में दो प्रमुख प्रकार होते हैं: Value Types और Reference Types। इन दोनों के बीच महत्वपूर्ण अंतर होता है:

1. Value Types (मानक प्रकार):

  • Value types वह प्रकार होते हैं जिनकी values variables में directly stored होती हैं। जब आप एक value type variable को दूसरे variable में assign करते हैं, तो variable की value copy हो जाती है, यानी दोनों variables अलग-अलग memory locations पर store होते हैं।

उदाहरण:

int a = 5;
int b = a; // b अब 5 की एक नई copy है, a और b अलग-अलग हैं
Value types की कुछ सामान्य examples हैं:

Primitive types (int, float, double, etc.)
Structs (जैसे DateTime, Guid)
Enumerations

2. Reference Types (संदर्भ प्रकार):

  • Reference types वे होते हैं जिनकी values variable में store नहीं होतीं। इसके बजाय, variable एक reference या pointer को store करता है, जो actual object की memory location की ओर इशारा करता है।
  •  जब आप एक reference type variable को दूसरे variable में assign करते हैं, तो दोनों variables उसी memory location को point करते हैं, यानी दोनों variables एक ही object को refer करते हैं।

उदाहरण:

class Person
{
    public string Name;
}

Person person1 = new Person();
person1.Name = "John";

Person person2 = person1; // person2 और person1 अब same memory location को point करते हैं
Reference types की कुछ सामान्य examples हैं:

Classes (जैसे Person, Car)
Arrays
Delegates
Strings (हालांकि strings को कभी-कभी value type की तरह treat किया जाता है)


Type Safety using Cast Operators :

C# में type safety सुनिश्चित करने के लिए cast operators का इस्तेमाल किया जाता है। Cast operator दो प्रकार के होते हैं:

Implicit Casting (Automatic Conversion): जब एक प्रकार का डेटा दूसरे प्रकार में आसानी से convert हो सकता है (जहां कोई data loss नहीं होता), तब implicit casting होती है। इसमें programmer को किसी explicit cast का इस्तेमाल करने की आवश्यकता नहीं होती।

उदाहरण:

int a = 10;
double b = a;  // Implicit casting from int to double
Explicit Casting (Manual Conversion): जब data types के बीच conversion करना होता है, और संभव हो कि डेटा का नुकसान हो (जैसे, float से int में cast करते समय decimal points का loss हो सकता है), तो explicit casting की जरूरत होती है। इसे C# में "cast operator" () द्वारा किया जाता है।

उदाहरण:

double a = 10.5;
int b = (int)a;  // Explicit casting from double to int, fractional part will be lost
Type Safety and Casts with Reference Types
Reference types के लिए भी cast operators का इस्तेमाल किया जा सकता है। जब आप एक base class को derived class में cast करते हैं, या एक class को दूसरी class में cast करते हैं, तब आपको type safety की जरूरत होती है।

Valid Cast: यदि दोनों types compatible हैं (जैसे एक class के object को उसी class के subclass में cast करना), तो यह सफल होता है।

उदाहरण:

class Animal { }
class Dog : Animal { }

Animal a = new Dog();
Dog d = (Dog)a;  // Valid cast

Invalid Cast: यदि types compatible नहीं हैं, तो यह runtime exception (InvalidCastException) throw करता है।

उदाहरण:

class Animal { }
class Cat : Animal { }

Animal a = new Dog();
Cat c = (Cat)a;  // Invalid cast, throws InvalidCastException
इससे बचने के लिए, C# में as keyword का इस्तेमाल किया जा सकता है, जो null return करता है यदि cast invalid है, बजाय exception के।

उदाहरण:

Cat c = a as Cat;  // Returns null instead of throwing an exception if cast is invalid

In this Chapter

Type Safety in Hindi
Introduction of .Net in Hindi
Web Services in Hindi | वेब सर्विसेस हिंदी में
WSDL in Hindi | WSDL हिंदी में
Boxing & Unboxing in ADO.NET in Hindi
CLR in Hindi | CLR क्या है?
Common Types System in Hindi
MSIL in Hindi
Assemblies & Class Libraries in Hindi
Project of .Net in Hindi
What is VB.NET and IDE in Hindi | वीबी.नेट क्या है ?
Intermediate Language in Hindi
Object Orientation in Hindi
Managed Execution in Hindi
Rapid Development in Hindi
Windows Presentation Foundation in Hindi
Whats new For .NET framework 3.5?
Windows Workflow Foundation (WWF) in Hindi
Windows Card Space in Hindi
Windows Communication Foundation in Hindi
How To Install and Use The Visual Studio 2008
How to Working With Visual Studio 2008
Types of Visual Studio 2008 in Hindi
Visual Studio 2008 IDE in Hindi
How To Create Console Application in Hindi
Introduction of C# in .NET in Hindi
Classes of .NET With C# in Hindi
Properties of .NET With C# in Hindi
Structs in C# .NET in Hindi
Delegates & Events in Hindi
Generic Collections in .NET (C#) in Hindi
Nullable Types in .NET in Hindi
ADO.NET in Hindi | ADO.NET क्या है?
SQL Connection Object in Hindi
SQL Command in Hindi
LINQ in Hindi | LINQ क्या है?
What is Using Stored Procedures?
Windows Application in .NET in Hindi | Windows Application क्या है?
BCA || .NET with C# 2023 Paper | MDSU Exam Paper
.NET with C# All Important Questions and Answers in Hindi (MDSU)
BCA || .NET with C# 2025 Paper | MDSU Exam Paper